我正在开发一个应用程序来显示sin wave。
我正在使用AudioQueueNewOutput来输出单声道声音,但是当我来到立体声输出时,我不知道该怎么做。
我知道mChannelsPerFrame = 2
可以在左右声道中生成wave。
我还想知道向左右声道发送字节的顺序是什么?第一个字节是左通道,第二个字节是右通道吗?
代码:
_audioFormat = new AudioStreamBasicDescription();
_audioFormat->mSampleRate = SAMPLE_RATE; // 44100
_audioFormat->mFormatID = kAudioFormatLinearPCM;
_audioFormat->mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
_audioFormat->mFramesPerPacket = 1;
_audioFormat->mChannelsPerFrame = NUM_CHANNELS; // 1
_audioFormat->mBitsPerChannel = BITS_PER_CHANNEL; // 16
_audioFormat->mBytesPerPacket = BYTES_PER_FRAME; // 2
_audioFormat->mBytesPerFrame = BYTES_PER_FRAME; // 2
和
_sineTableLength = _audioFormat.mSampleRate / SAMPLE_LIMIT_FACTOR; // 44100/100 = 441
_sineTable = new SInt16[_sineTableLength];
for(int i = 0; i < _sineTableLength; i++)
{
// Transfer values between -1.0 and 1.0 to integer values between -sample max and sample max
_sineTable[i] = (SInt16)(sin(i * 2 * M_PI / _sineTableLength) * 32767);
}
和
AudioQueueNewOutput (&_audioFormat,
playbackCallback,
(__bridge void *)(self),
nil,
nil,
0,
&_queueObject);
static void playbackCallback (void* inUserData,
AudioQueueRef inAudioQueue,
AudioQueueBufferRef bufferReference){
SInt16* sample = (SInt16*)bufferReference->mAudioData;
// bufferSize 1024
for(int i = 0; i < bufferSize; i += _audioFormat.mBytesPerFrame, sample++)
{
// set value for *sample
// 9ms sin wave and 4.5ms 0
...
}
...
AudioQueueEnqueueBuffer(...)
}
答案 0 :(得分:0)
几天后,我找到了答案。
首先:AudioStreamBasicDescription
可以设置为this;
然后:bufferSize
从1024
更改为2048
;
并且SInt16
中的SInt16* sample = (SInt16*)bufferReference->mAudioData;
全部更改为SInt32
。因为通道加倍,位加倍;
最后:每个16位包含sample
中左声道或右声道所需的数据,只需将其提供给您。