2

这是我从未见过的东西,但我在http://www.audiosynth.com/sinewavedemo.html下载了正弦波示例的源代码。

它是旧的 Project Builder 项目格式,我想用 Xcode (GCC) 编译它。但是,Xcode 给了我 11415 编译错误。前几个是(都在 AppKit.h 的预编译中):

/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:31:0 /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:31: error: expected identifier or '(' before '-' token


/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:33:0 /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:33: error: expected identifier or '(' before '-' token


/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:35:0 /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:35: error: expected identifier or '(' before '-' token


/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:36:0 /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:36: error: expected identifier or '(' before '-' token


/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:37:0 /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:37: error: expected identifier or '(' before '-' token


/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:38:0 /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:38: error: expected identifier or '(' before '-' token


/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:40:0 /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:40: error: expected identifier or '(' before '-' token


/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:42:0 /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:42: error: expected identifier or '(' before '@' token


/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:48:0 /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:48: error: expected identifier or '(' before '@' token


/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:54:0 /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:54: error: expected identifier or '(' before '@' token


/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:59:0 /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:59: error: expected identifier or '(' before '-' token


/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:61:0 /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:61: error: expected identifier or '(' before '@' token


/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:69:0 /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:69: error: expected identifier or '(' before '+' token


/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:71:0 /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:71: error: expected identifier or '(' before '+' token




/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSValue.h:39:0 /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSValue.h:39: error: expected identifier or '(' before '-' token


/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSValue.h:40:0 /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSValue.h:40: error: expected identifier or '(' before '-' token


/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSValue.h:41:0 /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSValue.h:41: error: expected identifier or '(' before '-' token


/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSValue.h:42:0 /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSValue.h:42: error: expected identifier or '(' before '-' token


/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSValue.h:43:0 /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSValue.h:43: error: expected identifier or '(' before '-' token


/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSValue.h:44:0 /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSValue.h:44: error: expected identifier or '(' before '-' token


/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSValue.h:45:0 /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSValue.h:45: error: expected identifier or '(' before '-' token


/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSValue.h:46:0 /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSValue.h:46: error: expected identifier or '(' before '-' token

一些代码是:

HAL.c

/*
 *  HAL.c
 *  Sinewave
 *
 *  Created by james on Fri Apr 27 2001.
 *  Copyright (c) 2001 __CompanyName__. All rights reserved.
 *
 */

#include "HAL.h"
#include "math.h"

appGlobals gAppGlobals;

OSStatus appIOProc (AudioDeviceID  inDevice, const AudioTimeStamp*  inNow, const AudioBufferList*   inInputData, 
                             const AudioTimeStamp*  inInputTime, AudioBufferList*  outOutputData, const AudioTimeStamp* inOutputTime, 
                             void* device);


#define FailIf(cond, handler)      \
 if (cond) {          \
  goto handler;        \
 }

#define FailWithAction(cond, action, handler)  \
 if (cond) {          \
  { action; }         \
  goto handler;        \
 }

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// HAL Sample Code ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//#define noErr 0
//#define false 0

OSStatus SetupHAL (appGlobalsPtr globals)
{
    OSStatus    err = noErr;
    UInt32    count,
                                        bufferSize;
    AudioDeviceID   device = kAudioDeviceUnknown;
    AudioStreamBasicDescription  format;

    // get the default output device for the HAL
    count = sizeof(globals->device);     // it is required to pass the size of the data to be returned
    err = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice,  &count, (void *) &device);
    fprintf(stderr, "kAudioHardwarePropertyDefaultOutputDevice %d\n", err);
    if (err != noErr) goto Bail;

    // get the buffersize that the default device uses for IO
    count = sizeof(globals->deviceBufferSize);    // it is required to pass the size of the data to be returned
    err = AudioDeviceGetProperty(device, 0, false, kAudioDevicePropertyBufferSize, &count, &bufferSize);
    fprintf(stderr, "kAudioDevicePropertyBufferSize %d %d\n", err, bufferSize);
    if (err != noErr) goto Bail;

    // get a description of the data format used by the default device
    count = sizeof(globals->deviceFormat);    // it is required to pass the size of the data to be returned
    err = AudioDeviceGetProperty(device, 0, false, kAudioDevicePropertyStreamFormat, &count, &format);
    fprintf(stderr, "kAudioDevicePropertyStreamFormat %d\n", err);
    fprintf(stderr, "sampleRate %g\n", format.mSampleRate);
    fprintf(stderr, "mFormatFlags %08X\n", format.mFormatFlags);
    fprintf(stderr, "mBytesPerPacket %d\n", format.mBytesPerPacket);
    fprintf(stderr, "mFramesPerPacket %d\n", format.mFramesPerPacket);
    fprintf(stderr, "mChannelsPerFrame %d\n", format.mChannelsPerFrame);
    fprintf(stderr, "mBytesPerFrame %d\n", format.mBytesPerFrame);
    fprintf(stderr, "mBitsPerChannel %d\n", format.mBitsPerChannel);
    if (err != kAudioHardwareNoError) goto Bail;
    FailWithAction(format.mFormatID != kAudioFormatLinearPCM, err = paramErr, Bail); // bail if the format is not linear pcm

    // everything is ok so fill in these globals
    globals->device = device;
    globals->deviceBufferSize = bufferSize;
    globals->deviceFormat = format;

Bail:
    return (err);
}

/*
struct AudioStreamBasicDescription
{
 Float64 mSampleRate;  // the native sample rate of the audio stream
 UInt32 mFormatID;   // the specific encoding type of audio stream
 UInt32 mFormatFlags;  // flags specific to each format
 UInt32 mBytesPerPacket; // the number of bytes in a packet
 UInt32 mFramesPerPacket; // the number of frames in each packet
 UInt32 mBytesPerFrame;  // the number of bytes in a frame
 UInt32 mChannelsPerFrame; // the number of channels in each frame
 UInt32 mBitsPerChannel; // the number of bits in each channel
};
typedef struct AudioStreamBasicDescription AudioStreamBasicDescription;
*/
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// This is a simple playThru ioProc. It simply places the data in the input buffer back into the output buffer.
// Watch out for feedback from Speakers to Microphone
OSStatus appIOProc (AudioDeviceID  inDevice, const AudioTimeStamp*  inNow, const AudioBufferList*   inInputData, 
                             const AudioTimeStamp*  inInputTime, AudioBufferList*  outOutputData, const AudioTimeStamp* inOutputTime, 
                             void* appGlobals)
{    
    appGlobalsPtr globals = appGlobals;
    int i;
    double phase = gAppGlobals.phase;
    double amp = gAppGlobals.amp;
    double pan = gAppGlobals.pan;
    double freq = gAppGlobals.freq * 2. * 3.14159265359 / globals->deviceFormat.mSampleRate;

    int numSamples = globals->deviceBufferSize / globals->deviceFormat.mBytesPerFrame;
    // assume floats for now....
    float *out = outOutputData->mBuffers[0].mData;
    for (i=0; i<numSamples; ++i) {
        float wave = sin(phase) * amp;
        phase = phase + freq;
        *out++ = wave * (1.0-pan);
        *out++ = wave * pan;
    }
    gAppGlobals.phase = phase;
    return (kAudioHardwareNoError);     
}



// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
OSStatus  StartPlayingThruHAL(appGlobalsPtr globals)
{
    OSStatus  err = kAudioHardwareNoError;

    if (globals->soundPlaying) return 0;
    globals->phase = 0.0;

    err = AudioDeviceAddIOProc(globals->device, appIOProc, (void *) globals); // setup our device with an IO proc
    if (err != kAudioHardwareNoError) goto Bail;

    err = AudioDeviceStart(globals->device, appIOProc);    // start playing sound through the device
    if (err != kAudioHardwareNoError) goto Bail;

    globals->soundPlaying = true;      // set the playing status global to true

Bail:
    return (err);
} 

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
OSStatus  StopPlayingThruHAL(appGlobalsPtr globals)
{
    OSStatus  err = kAudioHardwareNoError;

    if (!globals->soundPlaying) return 0;
    err = AudioDeviceStop(globals->device, appIOProc);    // stop playing sound through the device
    if (err != kAudioHardwareNoError) goto Bail;

    err = AudioDeviceRemoveIOProc(globals->device, appIOProc);   // remove the IO proc from the device
    if (err != kAudioHardwareNoError) goto Bail;

    globals->soundPlaying = false;      // set the playing status global to false

Bail:
    return (err);
} 

正弦波.m

//
// a very simple Cocoa CoreAudio app
// by James McCartney  james@audiosynth.com  www.audiosynth.com
//
// Sinewave - this class implements a sine oscillator with dezippered control of frequency, pan and amplitude
//

#import "Sinewave.h"

// define a C struct from the Obj-C object so audio callback can access data
typedef struct {
    @defs(Sinewave);
} sinewavedef;

// this is the audio processing callback.
OSStatus appIOProc (AudioDeviceID  inDevice, const AudioTimeStamp*  inNow, const AudioBufferList*   inInputData, 
                             const AudioTimeStamp*  inInputTime, AudioBufferList*  outOutputData, const AudioTimeStamp* inOutputTime, 
                             void* defptr)
{    
    sinewavedef* def = defptr; // get access to Sinewave's data
    int i;

    // load instance vars into registers
    double phase = def->phase;
    double amp = def->amp;
    double pan = def->pan;
    double freq = def->freq;

    double ampz = def->ampz;
    double panz = def->panz;
    double freqz = def->freqz;

    int numSamples = def->deviceBufferSize / def->deviceFormat.mBytesPerFrame;

    // assume floats for now....
    float *out = outOutputData->mBuffers[0].mData;

    for (i=0; i<numSamples; ++i) {

        float wave = sin(phase) * ampz;  // generate sine wave
        phase = phase + freqz;   // increment phase

        // write output
        *out++ = wave * (1.0-panz);  // left channel
        *out++ = wave * panz;   // right channel

        // de-zipper controls
        panz  = 0.001 * pan  + 0.999 * panz;
        ampz  = 0.001 * amp  + 0.999 * ampz;
        freqz = 0.001 * freq + 0.999 * freqz;
    }

    // save registers back to object
    def->phase = phase;
    def->freqz = freqz;
    def->ampz = ampz;
    def->panz = panz;

    return kAudioHardwareNoError;     
}


@implementation Sinewave

- (void) setup
{
    OSStatus    err = kAudioHardwareNoError;
    UInt32    count;    
    device = kAudioDeviceUnknown;

    initialized = NO;

    // get the default output device for the HAL
    count = sizeof(device);  // it is required to pass the size of the data to be returned
    err = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice,  &count, (void *) &device);
    if (err != kAudioHardwareNoError) {
     fprintf(stderr, "get kAudioHardwarePropertyDefaultOutputDevice error %ld\n", err);
        return;
    }

    // get the buffersize that the default device uses for IO
    count = sizeof(deviceBufferSize); // it is required to pass the size of the data to be returned
    err = AudioDeviceGetProperty(device, 0, false, kAudioDevicePropertyBufferSize, &count, &deviceBufferSize);
    if (err != kAudioHardwareNoError) {
     fprintf(stderr, "get kAudioDevicePropertyBufferSize error %ld\n", err);
        return;
    }
    fprintf(stderr, "deviceBufferSize = %ld\n", deviceBufferSize);

    // get a description of the data format used by the default device
    count = sizeof(deviceFormat); // it is required to pass the size of the data to be returned
    err = AudioDeviceGetProperty(device, 0, false, kAudioDevicePropertyStreamFormat, &count, &deviceFormat);
    if (err != kAudioHardwareNoError) {
     fprintf(stderr, "get kAudioDevicePropertyStreamFormat error %ld\n", err);
        return;
    }
    if (deviceFormat.mFormatID != kAudioFormatLinearPCM) {
     fprintf(stderr, "mFormatID !=  kAudioFormatLinearPCM\n");
        return;
    }
    if (!(deviceFormat.mFormatFlags & kLinearPCMFormatFlagIsFloat)) {
     fprintf(stderr, "Sorry, currently only works with float format....\n");
        return;
    }

    initialized = YES;

    fprintf(stderr, "mSampleRate = %g\n", deviceFormat.mSampleRate);
    fprintf(stderr, "mFormatFlags = %08lX\n", deviceFormat.mFormatFlags);
    fprintf(stderr, "mBytesPerPacket = %ld\n", deviceFormat.mBytesPerPacket);
    fprintf(stderr, "mFramesPerPacket = %ld\n", deviceFormat.mFramesPerPacket);
    fprintf(stderr, "mChannelsPerFrame = %ld\n", deviceFormat.mChannelsPerFrame);
    fprintf(stderr, "mBytesPerFrame = %ld\n", deviceFormat.mBytesPerFrame);
    fprintf(stderr, "mBitsPerChannel = %ld\n", deviceFormat.mBitsPerChannel);
}


- (void)setAmpVal:(double)val
{
    amp = val;
}

- (void)setFreqVal:(double)val
{
    freq = val * 2. * 3.14159265359 / deviceFormat.mSampleRate;
}

- (void)setPanVal:(double)val
{
    pan = val;
}

- (BOOL)start
{
    OSStatus  err = kAudioHardwareNoError;
   sinewavedef *def;

    if (!initialized) return false;
    if (soundPlaying) return false;

    // initialize phase and de-zipper filters.
    phase = 0.0;
    freqz = freq;
    ampz = amp;
    panz = pan;

    def = (sinewavedef *)self;

    err = AudioDeviceAddIOProc(device, appIOProc, (void *) def); // setup our device with an IO proc
    if (err != kAudioHardwareNoError) return false;

    err = AudioDeviceStart(device, appIOProc);    // start playing sound through the device
    if (err != kAudioHardwareNoError) return false;

    soundPlaying = true;      // set the playing status global to true
    return true;
}

- (BOOL)stop
{
    OSStatus  err = kAudioHardwareNoError;

    if (!initialized) return false;
    if (!soundPlaying) return false;

    err = AudioDeviceStop(device, appIOProc);    // stop playing sound through the device
    if (err != kAudioHardwareNoError) return false;

    err = AudioDeviceRemoveIOProc(device, appIOProc);   // remove the IO proc from the device
    if (err != kAudioHardwareNoError) return false;

    soundPlaying = false;      // set the playing status global to false
    return true;
}

@end

谁能帮我编译这个例子?我真的很感激。谢谢

4

2 回答 2

2

我的第一个猜测是,你在编译 C 代码时不知何故最终包含了 Objective-C 头文件。(NSObject.h 应该只包含在 .m 文件中。)将您的 .c 文件设置为编译为 Objective-C,或者确保您的 Prefix Header 文件不包含 Objective-C 框架头文件。

于 2010-06-12T04:36:17.263 回答
0

如果这是一个相对较旧的代码示例,您最好切换到 gcc 4.0 和旧的 SDK(例如 10.4u)。

于 2010-06-11T18:36:31.467 回答