Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

playback.txt 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*! \page playback Playback
  2. In this example, we provide a complete program that demonstrates the use of RtAudio for audio playback. Our program produces a two-channel sawtooth waveform for output.
  3. \code
  4. #include "RtAudio.h"
  5. #include <iostream>
  6. #include <cstdlib>
  7. // Two-channel sawtooth wave generator.
  8. int saw( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
  9. double streamTime, RtAudioStreamStatus status, void *userData )
  10. {
  11. unsigned int i, j;
  12. double *buffer = (double *) outputBuffer;
  13. double *lastValues = (double *) userData;
  14. if ( status )
  15. std::cout << "Stream underflow detected!" << std::endl;
  16. // Write interleaved audio data.
  17. for ( i=0; i<nBufferFrames; i++ ) {
  18. for ( j=0; j<2; j++ ) {
  19. *buffer++ = lastValues[j];
  20. lastValues[j] += 0.005 * (j+1+(j*0.1));
  21. if ( lastValues[j] >= 1.0 ) lastValues[j] -= 2.0;
  22. }
  23. }
  24. return 0;
  25. }
  26. int main()
  27. {
  28. RtAudio dac;
  29. if ( dac.getDeviceCount() < 1 ) {
  30. std::cout << "\nNo audio devices found!\n";
  31. exit( 0 );
  32. }
  33. RtAudio::StreamParameters parameters;
  34. parameters.deviceId = dac.getDefaultOutputDevice();
  35. parameters.nChannels = 2;
  36. parameters.firstChannel = 0;
  37. unsigned int sampleRate = 44100;
  38. unsigned int bufferFrames = 256; // 256 sample frames
  39. double data[2];
  40. try {
  41. dac.openStream( &parameters, NULL, RTAUDIO_FLOAT64,
  42. sampleRate, &bufferFrames, &saw, (void *)&data );
  43. dac.startStream();
  44. }
  45. catch ( RtError& e ) {
  46. e.printMessage();
  47. exit( 0 );
  48. }
  49. char input;
  50. std::cout << "\nPlaying ... press <enter> to quit.\n";
  51. std::cin.get( input );
  52. try {
  53. // Stop the stream
  54. dac.stopStream();
  55. }
  56. catch (RtError& e) {
  57. e.printMessage();
  58. }
  59. if ( dac.isStreamOpen() ) dac.closeStream();
  60. return 0;
  61. }
  62. \endcode
  63. We open the stream in exactly the same way as the previous example (except with a data format change) and specify the address of our callback function \e "saw()". The callback function will automatically be invoked when the underlying audio system needs data for output. Note that the callback function is called only when the stream is "running" (between calls to the RtAudio::startStream() and RtAudio::stopStream() functions). We can also pass a pointer value to the RtAudio::openStream() function that is made available in the callback function. In this way, it is possible to gain access to arbitrary data created in our \e main() function from within the globally defined callback function.
  64. In this example, we stop the stream with an explicit call to RtAudio::stopStream(). It is also possible to stop a stream by returning a non-zero value from the callback function. A return value of 1 will cause the stream to finish draining its internal buffers and then halt (equivalent to calling the RtAudio::stopStream() function). A return value of 2 will cause the stream to stop immediately (equivalent to calling the RtAudio::abortStream() function).
  65. */