Re: Trying to get all samples from an audio file on disk into memory
Re: Trying to get all samples from an audio file on disk into memory
- Subject: Re: Trying to get all samples from an audio file on disk into memory
- From: Yuriy Romanchenko <email@hidden>
- Date: Sun, 01 Mar 2015 19:26:40 +0200
> AudioBufferList *ioData;
> ioData = (AudioBufferList *)malloc(sizeof(AudioBufferList) + sizeof(AudioBuffer));
> ioData->mBuffers[0].mNumberChannels = 1;
> ioData->mBuffers[0].mDataByteSize = numberOfFrames * sizeof(SInt16);
You’re doing it wrong, why do you allocate 'sizeof(AudioBufferList) + sizeof(AudioBuffer)’ bytes? sizeof(AudioBufferList) will be enough for 1 buffer, instead you’re creating 2 buffers.
If you want to gather data into buffer list you need to do it in that way:
// ===
// Setup audio buffer list.
AudioBufferList ioData = {0};
ioData.mNumberBuffers = 1;
ioData.mBuffers[0].mNumberChannels = 1;
ioData.mBuffers[0].mDataByteSize = <#Your data size (in your case ‘packets * asbd.mBytesPerPacket ')#>;
ioData.mBuffers[0].mData = ioData.mBuffers[0].mDataByteSize;
// Read raw samples into buffer
ExtAudioFileRead(inputFile, &numberOfFrames, &ioData);
// You should get your samples in mData member of first buffer.
// … do stuff with samples… (But i highly recommend you to pass them to output to check that everything is fine till that step.)
// Cleanup
free(ioData.mBuffers[0].mData);
// ===
On Mar 1, 2015, at 19:02, Patrick J. Collins <email@hidden> wrote:
> I also noticed that it looks like I can do what I want with
> ExtAudioFile. I tried that out but I was just getting garbage audio.
>
> ExtAudioFileRef inputFile;
>
> CheckResult(ExtAudioFileOpenURL((__bridge CFURLRef)self.url,
> &inputFile),
> "ExtAudioFileOpenURL failed");
>
> UInt32 numberOfFrames = 0;
> UInt32 propSize = sizeof(SInt64);
> CheckResult(ExtAudioFileGetProperty(inputFile, kExtAudioFileProperty_FileLengthFrames, &propSize, &numberOfFrames), "GetProperty failed");
>
> AudioBufferList *ioData;
> ioData = (AudioBufferList *)malloc(sizeof(AudioBufferList) + sizeof(AudioBuffer));
> ioData->mBuffers[0].mNumberChannels = 1;
> ioData->mBuffers[0].mDataByteSize = numberOfFrames * sizeof(SInt16);
> ExtAudioFileRead(inputFile, &numberOfFrames, ioData);
>
> // do stuff with samples...
>
> free(ioData);
> ExtAudioFileDispose(inputFile);
>
> ...
>
> When I play back the samples, it just sounds like harsh white noise...
>
> Patrick J. Collins
> http://collinatorstudios.com
>
>
> On Sun, 1 Mar 2015, Yuriy Romanchenko wrote:
>
>> Hi,
>>
>> Assuming AIFF format you have got uncompressed lpcm samples there.
>> You can simply determine how much packets in that file by calling AudioFileGetProperty
>> with kAudioFilePropertyAudioDataPacketCount property id.
>> To determine length of audio file you should do the following:
>>
>> packets_per_second = sample_rate / frames_per_packet;
>> seconds = total_packet / packets_per_second
>>
>> For constant bit rate formats frames_per_packet will be equal to 1.
>>
>> Best regards,
>> Yuri.
>>
>> On Mar 1, 2015, at 5:51, Patrick J. Collins <email@hidden> wrote:
>>
>> either you want to use some new-fangled language and don't know how to do heap allocation or do you
>> don't understand how to
>> get the number of samples in the file?
>>
>>
>> That's correct.. I am not sure about a lot of how to properly get information like that via coreaudio's api...
>>
>> This would be my code to read samples to memory:
>>
>> AudioFileID file;
>> CheckError(AudioFileOpenURL((__bridge CFURLRef)self.url,
>> kAudioFileReadPermission,
>> 0,
>> &file),
>> "AudioFileOpenURL failed");
>>
>> AudioStreamBasicDescription dataFormat;
>> UInt32 propSize = sizeof(dataFormat);
>> CheckError(AudioFileGetProperty(file,
>> kAudioFilePropertyDataFormat,
>> &propSize,
>> &dataFormat),
>> "couldn't get file's data format");
>>
>>
>> UInt32 numBytes;
>> UInt32 numPackets;
>> float durationInSeconds = ???; // how do you determine the length of an audio file?
>>
>> NSUInteger samples = ceil(dataFormat.mSampleRate * durationInSeconds);
>> float *buffer = (float *) malloc (sizeof (float) * samples);
>> CheckError(AudioFileReadPacketData(file,
>> false,
>> &numBytes,
>> NULL,
>> 0,
>> &numPackets,
>> buffer),
>> "AudioFileReadData failed");
>>
>> So how can I determine the durationInSeconds?
>>
>> Patrick J. Collins
>> http://collinatorstudios.com
>> _______________________________________________
>> Do not post admin requests to the list. They will be ignored.
>> Coreaudio-api mailing list (email@hidden)
>> Help/Unsubscribe/Update your Subscription:
>>
>> This email sent to email@hidden
>>
>>
>>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Coreaudio-api mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden