ransomweaver

Forum Replies Created

Viewing 5 posts - 1 through 5 (of 5 total)

  • Author
    Posts
  • in reply to: AirPlay problem, sample rate? #1017019
    ransomweaver
    Participant

    I will look into that. Maybe the problem is my kind of audio session (mediaPlayback) but i need that to keep it alive in the background.

    in reply to: AirPlay problem, sample rate? #1017017
    ransomweaver
    Participant

    Well, I have two basic requirements;

    1) the app can play iPod music

    2) the app can play iPod music and the app’s own wav files in the background.

    And the only problem I have right now is that AirPlay doesn’t like an audio stream with 44.1 and 16k audio in it at the same time.

    I’m not sure how I would go about fixing that, even with a completely different way of playing audio files, without changing the hertz of one or the other of the kinds of files I’m playing.

    in reply to: AirPlay problem, sample rate? #1017016
    ransomweaver
    Participant
    in reply to: AirPlay problem, sample rate? #1017014
    ransomweaver
    Participant

    Hi,

    Thought as much. Actually I am doing an amplification of the file (using a method from a stack overflow thread you directed me to some time ago. In that method (which uses ExtAudioFile api) it reads a wav file, including the sample rate, then sets a AudioStreamBasicDescription object with the sample rate to indicate the format for the returned samples.  If I set that to 44100, the audio will actually sound fine on Airplay BUT the file is not complete; there is less than 1/2 the audio.

    i wonder if I could add in an interpolation into this method to raise the samples up to the number that should be in a 44.1khz file.

    Here’s the full method. Probably it involves doing this in the loop over the buffers. Any thoughts?

    <code>

    void ScaleAudioFileAmplitude(NSURL *theURL, float ampScale) {

    OSStatus err = noErr;

    ExtAudioFileRef audiofile;

    ExtAudioFileOpenURL((CFURLRef)theURL, &audiofile);

    assert(audiofile);

    // get some info about the file’s format.

    AudioStreamBasicDescription fileFormat;

    UInt32 size = sizeof(fileFormat);

    err = ExtAudioFileGetProperty(audiofile, kExtAudioFileProperty_FileDataFormat, &size, &fileFormat);

    // we’ll need to know what type of file it is later when we write

    AudioFileID aFile;

    size = sizeof(aFile);

    err = ExtAudioFileGetProperty(audiofile, kExtAudioFileProperty_AudioFile, &size, &aFile);

    AudioFileTypeID fileType;

    size = sizeof(fileType);

    err = AudioFileGetProperty(aFile, kAudioFilePropertyFileFormat, &size, &fileType);

    // tell the ExtAudioFile API what format we want samples back in

    AudioStreamBasicDescription clientFormat;

    bzero(&clientFormat, sizeof(clientFormat));

    clientFormat.mChannelsPerFrame = fileFormat.mChannelsPerFrame;

    clientFormat.mBytesPerFrame = 4;

    clientFormat.mBytesPerPacket = clientFormat.mBytesPerFrame;

    clientFormat.mFramesPerPacket = 1;

    clientFormat.mBitsPerChannel = 32;

    clientFormat.mFormatID = kAudioFormatLinearPCM;

    clientFormat.mSampleRate = fileFormat.mSampleRate;

    NSLog(@”Sample Rate is %1.2f”,clientFormat.mSampleRate);

    clientFormat.mFormatFlags = kLinearPCMFormatFlagIsFloat | kAudioFormatFlagIsNonInterleaved;

    err = ExtAudioFileSetProperty(audiofile, kExtAudioFileProperty_ClientDataFormat, sizeof(clientFormat), &clientFormat);

    // find out how many frames we need to read

    SInt64 numFrames = 0;

    size = sizeof(numFrames);

    err = ExtAudioFileGetProperty(audiofile, kExtAudioFileProperty_FileLengthFrames, &size, &numFrames);

    // create the buffers for reading in data

    AudioBufferList *bufferList = malloc(sizeof(AudioBufferList) + sizeof(AudioBuffer) * (clientFormat.mChannelsPerFrame – 1));

    bufferList->mNumberBuffers = clientFormat.mChannelsPerFrame;

    for (int ii=0; ii < bufferList->mNumberBuffers; ++ii) {

    bufferList->mBuffers[ii].mDataByteSize = sizeof(float) * numFrames;

    bufferList->mBuffers[ii].mNumberChannels = 1;

    bufferList->mBuffers[ii].mData = malloc(bufferList->mBuffers[ii].mDataByteSize);

    }

    // read in the data

    UInt32 rFrames = (UInt32)numFrames;

    err = ExtAudioFileRead(audiofile, &rFrames, bufferList);

    // close the file

    err = ExtAudioFileDispose(audiofile);

    // process the audio

    for (int ii=0; ii < bufferList->mNumberBuffers; ++ii) {

    float *fBuf = (float *)bufferList->mBuffers[ii].mData;

    for (int jj=0; jj < rFrames; ++jj) {

    *fBuf = *fBuf * ampScale;

    fBuf++;

    }

    }

    // open the file for writing

    err = ExtAudioFileCreateWithURL((CFURLRef)theURL, fileType, &fileFormat, NULL, kAudioFileFlags_EraseFile, &audiofile);

    // tell the ExtAudioFile API what format we’ll be sending samples in

    err = ExtAudioFileSetProperty(audiofile, kExtAudioFileProperty_ClientDataFormat, sizeof(clientFormat), &clientFormat);

    // write the data

    err = ExtAudioFileWrite(audiofile, rFrames, bufferList);

    // close the file

    ExtAudioFileDispose(audiofile);

    // destroy the buffers

    for (int ii=0; ii < bufferList->mNumberBuffers; ++ii) {

    free(bufferList->mBuffers[ii].mData);

    }

    free(bufferList);

    bufferList = NULL;

    }

    </code>

     

    in reply to: Boosting flite gain #1016002
    ransomweaver
    Participant

    Thanks!

    That SO question has the goods. I’m not much for C programming, but I know how to use a C function in an Obj C class, so I used the ScaleAudioFileAmplitude function shown there.

    This probably is a bit wasteful, as my modified flitecontroller writes the data to file then reads/writes it again with the amplification, but it works very well. And quicker than me figuring out how to iterate through the data part of the buffer in memory.

Viewing 5 posts - 1 through 5 (of 5 total)