ios - Writing and reading an audio file with Extended Audio File -
i m using extaudiofilewriteasync write audio file while using device recording, once recording finished try read extaudiofileread function , samples not same samples m writing... know why happen?
for writing:
self.audiomanager.inputblock = ^(float *data, uint32 numframes, uint32 numchannels) { (int = 0; < numframes*numchannels; i++) { printf("write*%f\n", data[i]); } uint32 numincomingbytes = numframes*numchannels*sizeof(float); uint32 *outputbuffer =(uint32*)malloc(numincomingbytes); memcpy(outputbuffer, recordeddata, numincomingbytes); audiobufferlist outgoingaudio; outgoingaudio.mnumberbuffers = 1; outgoingaudio.mbuffers[0].mnumberchannels = numchannels; outgoingaudio.mbuffers[0].mdatabytesize = numincomingbytes; outgoingaudio.mbuffers[0].mdata = self.outputbuffer; if( 0 == pthread_mutex_trylock( &outputaudiofilelock ) ) { extaudiofilewriteasync(outputfile, numframes, &outgoingaudio); } pthread_mutex_unlock( &outputaudiofilelock ); }; [self.audiomanager play];
for reading:
uint32 *outputbuffer = (uint32 *)malloc(numframes*numchannels*sizeof(float)); audiobufferlist converteddata; converteddata.mnumberbuffers = 1; converteddata.mbuffers[0].mnumberchannels = numchannels; converteddata.mbuffers[0].mdatabytesize = numframes*numchannels*sizeof(float); converteddata.mbuffers[0].mdata = outputbuffer; nsmutablearray *samplesarray = [[nsmutablearray alloc]init]; while (numframes > 0) { extaudiofileread(inputfile, &numframes, &converteddata); if (numframes > 0) { audiobuffer audiobuffer = converteddata.mbuffers[0]; float *samples = (float *)audiobuffer.mdata; (int = 0; < framecount*numchannels; i++) { printf("read*%f\n", samples[i]); } } }
by way i'm using novocaine project in order device audio. can reproduce saved audio novocaine code or other player.
when writing extaudiofileref output :
extaudiofilecreatewithurl(audiofileref, kaudiofilem4atype, &outputfiledesc, null, kaudiofileflags_erasefile, &outputfile);
where outputfiledesc
audiostreambasicdescription outputfiledesc = {44100.0, kaudioformatmpeg4aac, 0, 0, 1024, 0, thisnumchannels, 0, 0}; outputfiledesc.mformatflags = kaudioformatflagisbigendian | kaudioformatflagissignedinteger | kaudioformatflagispacked;
when reading extaudiofileref inputfile:
extaudiofileopenurl(audiofileref, &inputfile):
and in both cases (writing , reading) applied same format:
audiostreambasicdescription outputformat; _outputformat.msamplerate = self.samplingrate; _outputformat.mformatid = kaudioformatlinearpcm; _outputformat.mformatflags = kaudioformatflagisfloat; _outputformat.mbytesperpacket = 4*self.numchannels; _outputformat.mframesperpacket = 1; _outputformat.mbytesperframe = 4*self.numchannels; _outputformat.mchannelsperframe = self.numchannels; _outputformat.mbitsperchannel = 32; extaudiofilesetproperty(outputfile, kextaudiofileproperty_clientdataformat, sizeof(audiostreambasicdescription), &_outputformat); extaudiofilesetproperty(inputfile, kextaudiofileproperty_clientdataformat, sizeof(audiostreambasicdescription), &_outputformat);
and way, if read samples not equal written samples, mean value of both signals quite similar. not understand why not totally equal!
any idea i'm doing wrong?
it sounds there implicit format conversion 1 or both of extaudiofilerefs , seeing different samples result of conversion. have 3 formats: audio_in_format, file_format, , audio_out_format. if audio_in_format different file_format, writing extaudiofileref create audio converter convert input audio file_format before writing disk. , reading extaudiofileref create converter if file_format different audio_out_format.
opinion: it's confusing named writing extaudiofileref "outputfile", , reading extaudiofileref "inputfile". use audiowriter , audioreader.
Comments
Post a Comment