Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use AudioQueue to play a sound for Mac OSX in C++
    primarykey
    data
    text
    <p>I am trying to play a sound on OSX, from a buffer (eg: Equivalent of Windows "PlaySound" function).</p> <p>I have put together some C++ code to play audio with AudioQueue (as it is my understanding that this is the easiest way to play audio on OSX).</p> <p>However, no sound is ever generated, and the audio callback function is never called.</p> <p>Does anybody know what I'm doing wrong, or does anyone have a simple C/C++ example of how to play a sound on OSX?</p> <pre><code> #include #include #define BUFFER_COUNT 3 static struct AQPlayerState { AudioStreamBasicDescription desc; AudioQueueRef queue; AudioQueueBufferRef buffers[BUFFER_COUNT]; unsigned buffer_size; } state; static void audio_callback (void *aux, AudioQueueRef aq, AudioQueueBufferRef bufout) { printf("I never get called!\n"); #define nsamples 4096 short data[nsamples]; for (int i=0;imAudioDataByteSize = nsamples * sizeof(short) * 1; assert(bufout->mAudioDataByteSize mAudioData, data, bufout->mAudioDataByteSize); AudioQueueEnqueueBuffer(state.queue, bufout, 0, NULL); } void audio_init() { int i; bzero(&state, sizeof(state)); state.desc.mFormatID = kAudioFormatLinearPCM; state.desc.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked; state.desc.mSampleRate = 44100; state.desc.mChannelsPerFrame = 1; state.desc.mFramesPerPacket = 1; state.desc.mBytesPerFrame = sizeof (short) * state.desc.mChannelsPerFrame; state.desc.mBytesPerPacket = state.desc.mBytesPerFrame; state.desc.mBitsPerChannel = (state.desc.mBytesPerFrame*8)/state.desc.mChannelsPerFrame; state.desc.mReserved = 0; state.buffer_size = state.desc.mBytesPerFrame * state.desc.mSampleRate; if (noErr != AudioQueueNewOutput(&state.desc, audio_callback, 0, NULL, NULL, 0, &state.queue)) { printf("audioqueue error\n"); return; } // Start some empty playback so we'll get the callbacks that fill in the actual audio. for (i = 0; i mAudioDataByteSize = state.buffer_size; AudioQueueEnqueueBuffer(state.queue, state.buffers[i], 0, NULL); } if (noErr != AudioQueueStart(state.queue, NULL)) printf("AudioQueueStart failed\n"); printf("started audio\n"); } int main() { audio_init(); while (1) { printf("I can't hear anything!\n"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload