Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module patest_sine_hello;
- /** @file patest_sine.c
- @ingroup test_src
- @brief Play a sine wave for several seconds.
- @author Ross Bencina <rossb@audiomulch.com>
- @author Phil Burk <philburk@softsynth.com>
- @author D conversion: Jonas Kivi <satelliittipupu@y????.co.uk>
- @author D2 conversion: Andrej Mitrovic <andrej.mitrovich@gmail.com>
- */
- import portaudio;
- import std.math : sin;
- import std.stdio;
- import core.memory;
- import core.thread;
- const NUM_SECONDS = 1;
- const SAMPLE_RATE = 44100;
- // buffer size
- const FRAMES_PER_BUFFER = 64; // 64;
- const TABLE_SIZE = 200;
- struct paTestData
- {
- float[TABLE_SIZE] sine;
- int left_phase;
- int right_phase;
- char[20] message;
- }
- /* This routine will be called by the PortAudio engine when audio is needed.
- ** It may be called at interrupt level on some machines so don't do anything
- ** that could mess up the system like calling malloc() or free().
- */
- extern (C) static int patestCallback(void* inputBuffer, void* outputBuffer,
- uint framesPerBuffer,
- PaStreamCallbackTimeInfo* timeInfo,
- PaStreamCallbackFlags statusFlags,
- void* userData)
- {
- paTestData* data = cast(paTestData*) userData;
- float* toOut = cast(float*) outputBuffer;
- uint i;
- for (i = 0; i < framesPerBuffer; i++)
- {
- *toOut++ = data.sine[data.left_phase]; /* left */
- *toOut++ = data.sine[data.right_phase]; /* right */
- data.left_phase += 1;
- if (data.left_phase >= TABLE_SIZE)
- {
- data.left_phase -= TABLE_SIZE;
- }
- data.right_phase += 3; /* higher pitch so we can distinguish left and right. */
- if (data.right_phase >= TABLE_SIZE)
- {
- data.right_phase -= TABLE_SIZE;
- }
- }
- return PaStreamCallbackResult.paContinue;
- }
- /*
- * This routine is called by portaudio when playback is done.
- */
- extern (C) static void StreamFinished(void* userData)
- {
- paTestData* data = cast(paTestData*) userData;
- writeln("Stream Completed: ", data.message);
- }
- int main()
- {
- writeln("PortAudio test.");
- PaStreamParameters outputParameters;
- PaStream* stream;
- PaError err;
- paTestData data;
- int i;
- writeln("PortAudio Test: output sine wave. Sample rate: ", SAMPLE_RATE, " Buffer size: ", FRAMES_PER_BUFFER);
- /* initialise sinusoidal wavetable */
- for (i=0; i < TABLE_SIZE; i++)
- {
- data.sine[i] = cast(float) sin((cast(double) i / cast(double) TABLE_SIZE) * PI * 2.0);
- }
- data.left_phase = data.right_phase = 0;
- err = Pa_Initialize();
- if (err != PaErrorCode.paNoError)
- {
- goto error;
- }
- outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
- outputParameters.channelCount = 2; /* stereo output */
- outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
- outputParameters.suggestedLatency = Pa_GetDeviceInfo(outputParameters.device).defaultLowOutputLatency;
- outputParameters.hostApiSpecificStreamInfo = null;
- err = Pa_OpenStream(
- &stream,
- null, /* no input */
- &outputParameters,
- SAMPLE_RATE,
- FRAMES_PER_BUFFER,
- /* we won't output out of range samples so don't bother clipping them */
- paClipOff,
- &patestCallback,
- &data);
- if (err != PaErrorCode.paNoError)
- {
- goto error;
- }
- writeln(data.message, "No Message");
- err = Pa_SetStreamFinishedCallback(stream, &StreamFinished);
- if(err != PaErrorCode.paNoError) goto error;
- // Used as a precaution
- GC.disable();
- err = Pa_StartStream(stream);
- if( err != PaErrorCode.paNoError ) goto error;
- writeln("Play for seconds: ", NUM_SECONDS);
- Thread.sleep( NUM_SECONDS * 10_000_000 );
- GC.enable();
- ///~ // You can exit the infinite_loop if you're in a command line with CTRL-C.
- ///~ int infinite_loop = 1;
- ///~ while (infinite_loop == 1)
- ///~ {
- ///~ }
- err = Pa_StopStream(stream);
- if( err != PaErrorCode.paNoError ) goto error;
- err = Pa_CloseStream(stream);
- if( err != PaErrorCode.paNoError ) goto error;
- Pa_Terminate();
- writeln("Test finished.\n");
- return err;
- error:
- Pa_Terminate();
- writeln("An error occured while using the portaudio stream");
- writeln("Error number: ", err);
- writeln("Error message: ", Pa_GetErrorText(err));
- return err;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement