Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.




STK example 1: beep is an STK example that generates a beep.
The code was copied from https://ccrma.stanford.edu/software/stk/crealtime.html.
Note that original copy-pasted code resulted in multiple
compile warnings and that the
library style, design and example code looks
different from the experts' recommendations
Technical facts
Operating system(s) or programming environment(s)
IDE(s):
Project type:
C++ standard:
Compiler(s):
Libraries used:
STL: GNU ISO C++ Library, version 4.9.2
Qt project file: ./CppStkExample1/CppStkExample1.pro
./CppStkExample1/main.cpp
// crtsine.cpp STK tutorial program
#pragma GCC diagnostic push
// I (RJCB) wish I did not need to add these ignores...
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wunused-parameter"
#include "SineWave.h"
#include "RtAudio.h"
using namespace stk;
// This tick() function handles sample computation only. It will be
// called automatically when the system needs a new buffer of audio
// samples.
int tick( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
double streamTime, RtAudioStreamStatus status, void *dataPointer )
{
SineWave *sine = (SineWave *) dataPointer;
register StkFloat *samples = (StkFloat *) outputBuffer;
for ( unsigned int i=0; i<nBufferFrames; i++ )
*samples++ = sine->tick();
return 0;
}
int main()
{
// Set the global sample rate before creating class instances.
Stk::setSampleRate( 44100.0 );
SineWave sine;
RtAudio dac;
// Figure out how many bytes in an StkFloat and setup the RtAudio stream.
RtAudio::StreamParameters parameters;
parameters.deviceId = dac.getDefaultOutputDevice();
parameters.nChannels = 1;
RtAudioFormat format = ( sizeof(StkFloat) == 8 )
? RTAUDIO_FLOAT64 : RTAUDIO_FLOAT32;
unsigned int bufferFrames = RT_BUFFER_SIZE;
try {
dac.openStream( ¶meters, NULL, format,
(unsigned int)Stk::sampleRate(),
&bufferFrames, &tick, (void *)&sine );
}
catch ( RtError &error ) {
error.printMessage();
goto cleanup;
}
sine.setFrequency(440.0);
try {
dac.startStream();
}
catch ( RtError &error ) {
error.printMessage();
goto cleanup;
}
// Block waiting here.
char keyhit;
std::cout << "\nPlaying ... press <enter> to quit.\n";
std::cin.get( keyhit );
// Shut down the output stream.
try {
dac.closeStream();
}
catch ( RtError &error ) {
error.printMessage();
}
cleanup:
return 0;
}
#pragma GCC diagnostic pop
|
Go back to Richel Bilderbeek's C++ page.
Go back to Richel Bilderbeek's homepage.

This page has been created by the tool CodeToHtml