Skip to content

Instantly share code, notes, and snippets.

@alejolp
Created August 24, 2013 03:31
Show Gist options
  • Save alejolp/6325894 to your computer and use it in GitHub Desktop.
Save alejolp/6325894 to your computer and use it in GitHub Desktop.
portaudio issue
#include <iostream>
#include <cmath>
#include <portaudio.h>
#define SAMPLE_RATE 22050
int input_channels = 2; /* <<< THIS VALUE TO ZERO WORKS!! */
int output_channels = 2;
int dds_pos = 0;
static int portaudio_callback(const void *input, void *output,
unsigned long frameCount, const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags, void *userData)
{
int num_channels = 2;
int buffer_size = frameCount * num_channels;
int ch_idx;
float* foutput = static_cast<float*>(output);
for (int i=0; i<buffer_size; ) {
for (ch_idx=0; ch_idx<num_channels; ++ch_idx) {
foutput[i] = std::sin((2.0*M_PI*440.0*(dds_pos%SAMPLE_RATE))/SAMPLE_RATE);
i++;
}
dds_pos++;
}
return paContinue;
}
int main(int argc, char** argv) {
PaStream *stream;
PaError err;
err = Pa_Initialize();
if( err != paNoError ) {
std::cerr << "PortAudio error: " << Pa_GetErrorText( err ) << std::endl;
return 0;
}
PaHostApiIndex api_idx;
const PaHostApiInfo* info = Pa_GetHostApiInfo(Pa_GetDefaultHostApi());
std::cerr << "Default device: " << info->name << std::endl;
for (api_idx = 0; api_idx < Pa_GetHostApiCount(); ++api_idx) {
info = Pa_GetHostApiInfo(api_idx);
std::cerr << "device " << api_idx << ": " << info->name << std::endl;
}
//int frames_per_buffer = SAMPLE_RATE;
int frames_per_buffer = paFramesPerBufferUnspecified;
/* Open an audio I/O stream. */
err = Pa_OpenDefaultStream( &stream,
input_channels, /* input channels */
output_channels, /* output */
paFloat32, /* 32 bit floating point output */
SAMPLE_RATE,
frames_per_buffer, /* frames per buffer, i.e. the number
of sample frames that PortAudio will
request from the callback. Many apps
may want to use
paFramesPerBufferUnspecified, which
tells PortAudio to pick the best,
possibly changing, buffer size.*/
portaudio_callback, /* this is your callback function */
static_cast<void*>(0) ); /*This is a pointer that will be passed to
your callback*/
if( err != paNoError ) {
std::cerr << "PortAudio error: " << Pa_GetErrorText( err ) << std::endl;
return 0;
}
err = Pa_StartStream( stream );
if( err != paNoError ) {
std::cerr << "PortAudio error: " << Pa_GetErrorText( err ) << std::endl;
return 0;
}
for (int i=0;i<10;++i) {
Pa_Sleep(1000);
}
err = Pa_StopStream(stream);
if (err != paNoError) {
std::cerr << "PortAudio error: " << Pa_GetErrorText(err) << std::endl;
return 0;
}
err = Pa_CloseStream(stream);
if (err != paNoError) {
std::cerr << "PortAudio error: " << Pa_GetErrorText(err) << std::endl;
return 0;
}
err = Pa_Terminate();
if (err != paNoError) {
std::cerr << "PortAudio error: " << Pa_GetErrorText(err) << std::endl;
return 0;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment