1

I am working on online radio app I managed to play the streamed mp3 packets from the Icecast server using AudioQueueServices, what I am struggling with is implementing a recording feature.

Since the streaming is in mp3 format I can not write the Audio packets directly to file using AudioFileWritePackets.

To leverage The automatic conversion of Extended Audio I am using ExtAudioWriteFile to write to a wav file. I have setup the AudioStreamBasicDescription of the incoming packets using the FileStreamOpen call back function AudioFileStream_PropertyListenerProc and the destination format I populated manually.The code successfully creates the file and writes the packet to it but on playback what I hear is a white noise; Here is my code

// when the recording button is pressed this function creates the file and setup the asbd
    -(void)startRecording{
        recording = true;
        OSStatus status;
        NSURL *baseUrl=[self applicationDocumentsDirectory];//returns the document direcotry of the app
        NSURL *audioUrl = [NSURL URLWithString:@"Recorded.wav" relativeToURL:baseUrl];
       //asbd setup for the destination file/wav file
        AudioStreamBasicDescription dstFormat;
        dstFormat.mSampleRate=44100.0;
        dstFormat.mFormatID=kAudioFormatLinearPCM;   dstFormat.mFormatFlags=kAudioFormatFlagsNativeEndian|kAudioFormatFlagIsSignedInteger|kAudioFormatFlagIsPacked;
        dstFormat.mBytesPerPacket=4;
        dstFormat.mBytesPerFrame=4;
        dstFormat.mFramesPerPacket=1;
        dstFormat.mChannelsPerFrame=2;
        dstFormat.mBitsPerChannel=16;
        dstFormat.mReserved=0;
        //creating the file    
        status = ExtAudioFileCreateWithURL(CFBridgingRetain(audioUrl), kAudioFileWAVEType, &(dstFormat), NULL, kAudioFileFlags_EraseFile, &recordingFilRef);

        // tell the EXtAudio File ApI  what format we will be sending samples
        //recordasbd is the asbd of incoming packets populated in  AudioFileStream_PropertyListenerProc
        status =   ExtAudioFileSetProperty(recordingFilRef, kExtAudioFileProperty_ClientDataFormat, sizeof(recordasbd), &recordasbd);  
    }
    // a handler called by packetproc call back function in AudiofileStreamOpen 
    - (void)handlePacketsProc:(const void *)inInputData numberBytes:(UInt32)inNumberBytes numberPackets:(UInt32)inNumberPackets packetDescriptions:(AudioStreamPacketDescription *)inPacketDescriptions {
    if(recording){
         // wrap the destination buffer in an audiobuffer list
         convertedData.mNumberBuffers= 1;
         convertedData.mBuffers[0].mNumberChannels = recordasbd.mChannelsPerFrame;
         convertedData.mBuffers[0].mDataByteSize = inNumberBytes;
         convertedData.mBuffers[0].mData = inInputData;


       ExtAudioFileWrite(recordingFilRef,recordasbd.mFramesPerPacket * inNumberPackets, &convertedData);
         }

    }

My questions are:

Is my approach right can I write mp3 packets to wav file this way If so what am I missing ??

If my approach is wrong please tell me any other way you think is right.A nudge in the right direction is more than enough for me

I am so grateful for any help I have read every SO question I could get my hands on this topic, I also looked closely at apples Convertfile example but I could not figure out what I am missng Thanks in advance for any help

1 Answers1

0

Why not write the raw mp3 packets directly to a file? Without using ExtAudioFile at all.

They will form a valid mp3 file and will be much smaller than the equivalent wav file.

Rhythmic Fistman
  • 34,352
  • 5
  • 87
  • 159
  • I want to write to mp3 but due to copyright issue apple doesn't support mp3 .AudiofileCreateWithurl returns status 'fmt?'(kAudioConverterErr_FormatNotSupported) that is why i am trying to convert it to wav . – Behailu Negash May 07 '17 at 18:33
  • I mean write the raw packets to a file using `fwrite`, `write` or `NSFileHandle`'s methods. Apple doesn't support encoding mp3 data, for reasons they haven't specified, but you don't need an encoder as you already have mp3 data. They _do_ support decoding it. – Rhythmic Fistman May 07 '17 at 22:44