在Cocoa中播放正弦波音

时间:2016-02-22 19:02:20

标签: objective-c macos cocoa audio

更新:找到答案,见下文。)

我正在尝试在Objective-C Cocoa应用程序中播放1 kHz正弦波音调;我(尝试)将一个Swift示例转换为Objective-C,但某处必定存在错误,因为结果音调大约为440 Hz而不是1 kHz,并且仅在左侧通道上。

代码:

@property (nonatomic, strong) AVAudioEngine *audioEngine;
@property (nonatomic, strong) AVAudioPlayerNode *player;
@property (nonatomic, strong) AVAudioMixerNode *mixer;
@property (nonatomic, strong) AVAudioPCMBuffer *buffer;

// -----

self.audioEngine = [[AVAudioEngine alloc] init];
self.player = [[AVAudioPlayerNode alloc] init];
self.mixer = self.audioEngine.mainMixerNode;
self.buffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:[self.player outputFormatForBus:0] frameCapacity:100];
self.buffer.frameLength = 100;

float amplitude = 0.4;
float frequency = 1000;
float sampleRate = [[self.mixer outputFormatForBus:0] sampleRate];
NSInteger channelCount = [[self.mixer outputFormatForBus:0] channelCount];


float *const *floatChannelData = self.buffer.floatChannelData;
float *p2 = *floatChannelData;

NSLog(@"Sine generator: sample rate = %.1f, %ld channels, frame length = %u.", sampleRate, (long)channelCount, self.buffer.frameLength);

for (int i = 0; i < self.buffer.frameLength ; i += channelCount) {

    // a = Amplitude
    // n = current sample
    // r = Sample rate (samples / sec.)
    //
    // f(n) = a * sin( theta(n) )
    // where theta(n) = 2 * M_PI * n / r

    float theta = 441.0f * i * 2.0 * M_PI / sampleRate;
    float value = sinf(theta);

    p2[i] = value * amplitude;
}


[self.audioEngine attachNode:self.player];
[self.audioEngine connect:self.player to:self.mixer format:[self.player outputFormatForBus:0]];
[self.audioEngine startAndReturnError:nil];

[self.player play];
[self.player scheduleBuffer:self.buffer atTime:nil options:AVAudioPlayerNodeBufferLoops completionHandler:nil];

我怀疑float theta=...行中有数学错误,或者我在floatChannelData缓冲区出错了。最初的Swift行写道:

buffer.floatChannelData.memory[i] = val * 0.5

不确定float *const *类型floatChannelData的确切内容。我的理解是这是一个指向2 x float * const数组的指针。 (2因为频道数量,左/右。)

Swift代码的来源是:http://www.tmroyal.com/playing-sounds-in-swift-audioengine.html

如果有人能向我解释缓冲结构,那将是非常好的。

找到解决方案

问题是双重的。首先,值441.0确实控制了频率。但仅仅改变这一点并没有解决问题;由此产生的音调比正弦更像锯齿状,并找出原因。

因子441和采样率44.1 kHz,这些值的比率是1:100 - 恰好是缓冲区中的样本数。将441更改为不是整数倍的值会导致“不完整”的正弦波:最后一个样本帧(#100)中的值不为零,这会在循环重新开始时导致急剧下降 - 这听起来像是一个锯齿波。

我必须将帧缓冲区长度更改为频率与采样率之比的精确(或倍数),以便最后一个样本值为(接近)零。

更新的代码:

self.audioEngine = [[AVAudioEngine alloc] init];
self.player = [[AVAudioPlayerNode alloc] init];
self.mixer = self.audioEngine.mainMixerNode;

float sampleRate = [[self.mixer outputFormatForBus:0] sampleRate];
AVAudioFrameCount frameBufferLength = floor(sampleRate / self.frequency) * 1;

self.buffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:[self.player outputFormatForBus:0] frameCapacity:frameBufferLength];
self.buffer.frameLength = frameBufferLength;

NSInteger channelCount = [[self.mixer outputFormatForBus:0] channelCount];

float *const *floatChannelData = self.buffer.floatChannelData;    

NSLog(@"Sine generator: sample rate = %.1f, %ld channels, frame length = %u.", sampleRate, (long)channelCount, self.buffer.frameLength);

for (int i = 0; i < self.buffer.frameLength ; i ++) {
    float theta = self.frequency * i * 2.0 * M_PI / sampleRate;
    float value = sinf(theta);
    for (int channelNumber = 0; channelNumber < channelCount ; channelNumber++) {
        float * const channelBuffer = floatChannelData[channelNumber];
        channelBuffer[i] = value * self.amplitude;
    }   
}

这样也可以正确处理任意数量的频道。

1 个答案:

答案 0 :(得分:2)

频率部分很简单:计算441.0f时的文字theta会控制它,所以只需将其更改为您想要的任何内容。

对于单声道问题,您似乎只是在编写一个数据通道:p2[i] = value * amplitude;如果您对floatChannelData的构图是正确的,那么您想要这个:

float * const * floatChannelData = self.buffer.floatChannelData;
float * const left = floatChannelData[0];
float * const right = floatChannelData[1];

//...

// N.B. Changed the increment
for (int i = 0; i < self.buffer.frameLength ; i++ ) {

    // ...

    left[i] = value * amplitude;
    right[i] = value * amplitude;
}

但是,考虑到for循环中的增量步骤,您的缓冲区可能是交错的(左右声道在同一缓冲区中交替)。在这种情况下,你保持循环增量,但在每一步都写入p2[i]p2[i+1](对于立体声很容易;如果你有更多的通道,你会对这些进行内循环并写入从{0}到$ NUM_CHANNELS的p2[j]j