如何在iPhone / Mac上使用CoreAudio合成声音?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (41)

我想在iPhone中播放合成声音。而不是使用预先录制的声音并使用SystemSoundID播放现有的二进制文件,我想综合它。部分原因是,这是因为我希望能够连续播放声音(例如,当用户的手指在屏幕上)而不是一次性声音样本。

如果我想合成中间A + 1(A4)(440Hz),我可以用sin()计算正弦波。我不知道的是如何将这些比特放入CoreAudio可以播放的数据包中。网络上的大多数教程都只关注现有的二进制文件。

任何人都可以用440Hz的简单合成正弦波来帮助我吗?

提问于
用户回答回答于

你想要做什么可能设置一个AudioQueue。它允许你在回调中填充合成音频数据的缓冲区。你可以将AudeioQueue设置为在新线程中运行,如下所示:

#define BUFFER_SIZE 16384
#define BUFFER_COUNT 3
static AudioQueueRef audioQueue;
void SetupAudioQueue() {
    OSStatus err = noErr;
    // Setup the audio device.
    AudioStreamBasicDescription deviceFormat;
    deviceFormat.mSampleRate = 44100;
    deviceFormat.mFormatID = kAudioFormatLinearPCM;
    deviceFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
    deviceFormat.mBytesPerPacket = 4;
    deviceFormat.mFramesPerPacket = 1;
    deviceFormat.mBytesPerFrame = 4;
    deviceFormat.mChannelsPerFrame = 2;
    deviceFormat.mBitsPerChannel = 16;
    deviceFormat.mReserved = 0;
    // Create a new output AudioQueue for the device.
    err = AudioQueueNewOutput(&deviceFormat, AudioQueueCallback, NULL,
                              CFRunLoopGetCurrent(), kCFRunLoopCommonModes,
                              0, &audioQueue);
    // Allocate buffers for the AudioQueue, and pre-fill them.
    for (int i = 0; i < BUFFER_COUNT; ++i) {
        AudioQueueBufferRef mBuffer;
        err = AudioQueueAllocateBuffer(audioQueue, BUFFER_SIZE, mBuffer);
        if (err != noErr) break;
        AudioQueueCallback(NULL, audioQueue, mBuffer);
    }
    if (err == noErr) err = AudioQueueStart(audioQueue, NULL);
    if (err == noErr) CFRunLoopRun();
  }

你只要AudioQueue需要更多数据,就会调用AudioCueueCallback方法。用类似的东西来实现:

void AudioQueueCallback(void* inUserData, AudioQueueRef inAQ,
                        AudioQueueBufferRef inBuffer) {
    void* pBuffer = inBuffer->mAudioData;
    UInt32 bytes = inBuffer->mAudioDataBytesCapacity;
    // Write max <bytes> bytes of audio to <pBuffer>
    outBuffer->mAudioDataByteSize = actualNumberOfBytesWritten
    err = AudioQueueEnqueueBuffer(audioQueue, inBuffer, 0, NULL);
}
用户回答回答于

//
//  AudioUnitTestAppDelegate.m
//  AudioUnitTest
//
//  Created by Marc Vaillant on 11/25/08.
//  Copyright __MyCompanyName__ 2008. All rights reserved.
//

#import "AudioUnitTestAppDelegate.h"
#include <AudioUnit/AudioUnit.h>
//#include "MachTimer.hpp"
#include <vector>
#include <iostream>

using namespace std;

#define kOutputBus 0
#define kInputBus 1
#define SAMPLE_RATE 44100

vector<int> _pcm;
int _index;

@implementation AudioUnitTestAppDelegate

@synthesize window;

void generateTone(
                vector<int>& pcm, 
                int freq, 
                double lengthMS, 
                int sampleRate, 
                double riseTimeMS, 
                double gain)
{
  int numSamples = ((double) sampleRate) * lengthMS / 1000.;
  int riseTimeSamples = ((double) sampleRate) * riseTimeMS / 1000.;

  if(gain > 1.)
    gain = 1.;
  if(gain < 0.)
    gain = 0.;

  pcm.resize(numSamples);

  for(int i = 0; i < numSamples; ++i)
  {
    double value = sin(2. * M_PI * freq * i / sampleRate);
    if(i < riseTimeSamples)
      value *= sin(i * M_PI / (2.0 * riseTimeSamples));
    if(i > numSamples - riseTimeSamples - 1)
      value *= sin(2. * M_PI * (i - (numSamples - riseTimeSamples) + riseTimeSamples)/ (4. * riseTimeSamples));

    pcm[i] = (int) (value * 32500.0 * gain);
    pcm[i] += (pcm[i]<<16);
  }

}

static OSStatus playbackCallback(void *inRefCon, 
                                  AudioUnitRenderActionFlags *ioActionFlags, 
                                  const AudioTimeStamp *inTimeStamp, 
                                  UInt32 inBusNumber, 
                                  UInt32 inNumberFrames, 
                                  AudioBufferList *ioData) 
{    
    cout<<"index = "<<_index<<endl;
    cout<<"numBuffers = "<<ioData->mNumberBuffers<<endl;

    int totalNumberOfSamples = _pcm.size();
    for(UInt32 i = 0; i < ioData->mNumberBuffers; ++i)
    {
      int samplesLeft = totalNumberOfSamples - _index;
      int numSamples = ioData->mBuffers[i].mDataByteSize / 4;
      if(samplesLeft > 0)
      {
        if(samplesLeft < numSamples)
        {
          memcpy(ioData->mBuffers[i].mData, &_pcm[_index], samplesLeft * 4);
          _index += samplesLeft;
          memset((char*) ioData->mBuffers[i].mData + samplesLeft * 4, 0, (numSamples - samplesLeft) * 4) ;
        }
        else
        {
          memcpy(ioData->mBuffers[i].mData, &_pcm[_index], numSamples * 4) ;
          _index += numSamples;
        }
      }
      else
        memset(ioData->mBuffers[i].mData, 0, ioData->mBuffers[i].mDataByteSize);
    }

    return noErr;
}

- (void)applicationDidFinishLaunching:(UIApplication *)application 
{    
  //generate pcm tone  freq = 800, duration = 1s, rise/fall time = 5ms

  generateTone(_pcm, 800, 1000, SAMPLE_RATE, 5, 0.8);
  _index = 0;

  OSStatus status;
  AudioComponentInstance audioUnit;

  // Describe audio component
  AudioComponentDescription desc;
  desc.componentType = kAudioUnitType_Output;
  desc.componentSubType = kAudioUnitSubType_RemoteIO;
  desc.componentFlags = 0;
  desc.componentFlagsMask = 0;
  desc.componentManufacturer = kAudioUnitManufacturer_Apple;

  // Get component
  AudioComponent inputComponent = AudioComponentFindNext(NULL, &desc);

  // Get audio units
  status = AudioComponentInstanceNew(inputComponent, &audioUnit);
  //checkStatus(status);

  UInt32 flag = 1;
  // Enable IO for playback
  status = AudioUnitSetProperty(audioUnit, 
                  kAudioOutputUnitProperty_EnableIO, 
                  kAudioUnitScope_Output, 
                  kOutputBus,
                  &flag, 
                  sizeof(flag));
  //checkStatus(status);

  // Describe format

  AudioStreamBasicDescription audioFormat;
  audioFormat.mSampleRate = SAMPLE_RATE;
  audioFormat.mFormatID = kAudioFormatLinearPCM;
  audioFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
  audioFormat.mFramesPerPacket = 1;
  audioFormat.mChannelsPerFrame = 2;
  audioFormat.mBitsPerChannel = 16;
  audioFormat.mBytesPerPacket = 4;
  audioFormat.mBytesPerFrame = 4;

  // Apply format

  status = AudioUnitSetProperty(audioUnit, 
                  kAudioUnitProperty_StreamFormat, 
                  kAudioUnitScope_Input, 
                  kOutputBus, 
                  &audioFormat, 
                  sizeof(audioFormat));
//  checkStatus(status);

  // Set output callback
  AURenderCallbackStruct callbackStruct;
  callbackStruct.inputProc = playbackCallback;
  callbackStruct.inputProcRefCon = self;
  status = AudioUnitSetProperty(audioUnit, 
                  kAudioUnitProperty_SetRenderCallback, 
                  kAudioUnitScope_Global, 
                  kOutputBus,
                  &callbackStruct, 
                  sizeof(callbackStruct));

  // Initialize
  status = AudioUnitInitialize(audioUnit);

  // Start playing

  status = AudioOutputUnitStart(audioUnit);

  [window makeKeyAndVisible];
}


- (void)dealloc {
    [window release];
    [super dealloc];
}


@end

扫码关注云+社区