Tony Wang
Tony Wang

Reputation: 1021

core audio how to generate a square wave with two channel ( stereo)

It's the code from Learning Core Audio http://www.amazon.com/Learning-Core-Audio-Hands-On-Programming/dp/0321636848
Just like the book says ,generating stereo should set asbd.mBitsPerChannel to 8 and asbd.mChannelsPerFrame to 2 . But the audio I get only has one channel . I don't know what's wrong with the code ,please help me . Thanks

#import <Foundation/Foundation.h>
#import <AudioToolbox/AudioToolbox.h>

#define SAMPLE_RATE 44100
#define DURATION 5.0
#define FILENAME_FORMAT @"%0.3f-square.aif"

int main(int argc, const char * argv[])
{

@autoreleasepool {
    if (argc < 2) {
        printf("Usage: CAToneFileGenerator n\n(where n is tone in Hz)");
        return -1;
    }

    double hz = atof(argv[1]);
    assert(hz > 0);
    NSLog(@"generating %f hz tone", hz);

    NSString *fileName = [NSString stringWithFormat:FILENAME_FORMAT, hz];
    NSString *filePath = [[[NSFileManager defaultManager] currentDirectoryPath] stringByAppendingPathComponent:fileName];
    NSLog(@"%@", filePath);
    NSURL *fileURL = [NSURL fileURLWithPath:filePath];

    AudioStreamBasicDescription asbd;
    memset(&asbd, 0, sizeof(asbd));
    asbd.mSampleRate = SAMPLE_RATE;
    asbd.mFormatID = kAudioFormatLinearPCM;
    asbd.mFormatFlags = kAudioFormatFlagIsBigEndian | kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
    asbd.mBitsPerChannel = 8;
    asbd.mChannelsPerFrame = 2;
    asbd.mFramesPerPacket = 1;
    asbd.mBytesPerFrame = 2;
    asbd.mBytesPerPacket = 2;

    AudioFileID audioFile;
    OSStatus audioErr = noErr;
    audioErr = AudioFileCreateWithURL((CFURLRef) fileURL, kAudioFileAIFFType, &asbd, kAudioFileFlags_EraseFile, &audioFile);
    assert(audioErr == noErr);

    long maxSampleCount = SAMPLE_RATE * DURATION;
    long sampleCount = 0;
    UInt32 bytesToWrite = 2;
    double wavelengthInSamples = SAMPLE_RATE / hz;

    while (sampleCount < maxSampleCount) {
        for (int i = 0; i < wavelengthInSamples; i++) {
            SInt16 sample;
            if (i < wavelengthInSamples /2) {
                sample = CFSwapInt16HostToBig(SHRT_MAX);
            }else{
                sample = CFSwapInt16HostToBig(SHRT_MIN);
            }

            audioErr = AudioFileWriteBytes(audioFile, false, sampleCount*2, &bytesToWrite, &sample);
            assert(audioErr == noErr);
            sampleCount ++;
        }
    }
    audioErr = AudioFileClose(audioFile);
    assert(audioErr == noErr);
    NSLog(@"wrote %ld samples", sampleCount);
}
return 0;
}

Upvotes: 2

Views: 579

Answers (1)

invalidname
invalidname

Reputation: 3185

Just changing the ASBD from the book code doesn't magically fix everything. You haven't accounted for how you're writing the samples to the file. Also, 8-bit is going to sound like ass.

Go back to mBitsPerChannel = 16, and then account for the fact you're writing two channels per frame, meaning that mBytesPerFrame and mBytesPerPacket will now be 4 (they were 2 in the book). Think about why this is.

Then you should just be able to add a second call to AudioFileWriteBytes() -- or do a loop where you count over mChannelsPerFrame -- right after the first one. But you'll have to account for the different offsets in the file, since you're writing 4 bytes each pass instead of 2. I think this is right:

  audioErr = AudioFileWriteBytes(audioFile, false, sampleCount*4, &bytesToWrite, &sample); // left
  audioErr = AudioFileWriteBytes(audioFile, false, (sampleCount*4)+2, &bytesToWrite, &sample); // right

You need to figure out some of this stuff on your own in order for it to sink in.

Upvotes: 2

Related Questions