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.

75 lines
3.9KB

  1. /*! \page crealtime Realtime Audio (callback)
  2. An alternative scheme for audio input/output is to define a specific
  3. function in which audio computations are performed and to let the
  4. audio system call this function when more input/output data can be
  5. accepted by the hardware (referred to as a callback scheme). In this
  6. section, we show how the previous <TT>rtsine.cpp</TT> program can be
  7. modified to work in a callback scenario. There is no "single-sample"
  8. interface for this functionality. The callback function will be
  9. invoked automatically by the audio system controller (RtAudio) when
  10. new data is needed and it is necessary to compute a full audio buffer
  11. of samples at that time (see \ref callback for further information).
  12. The previous section described the use of the stk::RtWvOut class for
  13. realtime audio output. The stk::RtWvOut::tick() function writes data to a
  14. large ring-buffer, from which data is periodically written to the
  15. computer's audio hardware via an underlying callback routine.
  16. \include crtsine.cpp
  17. The sinusoidal oscillator is created as before. The instantiation of
  18. RtAudio requires quite a few more parameters, including output/input
  19. device and channel specifiers, the data format, and the desired buffer
  20. length (in frames). In this example, we request a single output
  21. channel using the default output device, zero channels of input, the
  22. RtAudio data format which corresponds to an <tt>StkFloat</tt>, and the
  23. RT_BUFFER_SIZE defined in Stk.h. The \c bufferFrames argument is an
  24. API-dependent buffering parameter (see RtAudio for further
  25. information).
  26. We also provide the audio system controller with a pointer to our
  27. callback function and an optional pointer to data that will be made
  28. available in the callback. In this example, we need to pass only the
  29. pointer to the oscillator. In more complex programs, it is typically
  30. necessary to put all shared data in a <tt>struct</tt> (see the next
  31. tutorial program for an example) or make use of global variables.
  32. Our callback routine is the \c tick() function. Function arguments
  33. include pointers to the audio input and output data buffers, the
  34. buffer size (in frames), a stream time argument, a status argument to
  35. test for over/underruns, and the data pointer passed in the
  36. openStream() function (if it exists). It is necessary to cast these
  37. pointers to their corresponding data types before use. Our tick()
  38. routine simply "ticks" the oscillator for \c nBufferFrames counts and
  39. writes the result into the audio data buffer before returning.
  40. The \c main() function blocks at the std::cin.get() call until the
  41. user hits the "enter" key, after which the audio controller is shut
  42. down and program execution ends.
  43. \section callback Blocking vs. Callbacks
  44. Prior to version 4.2.0, all STK example projects and programs used
  45. blocking audio input/output functionality (typically with the RtWvIn,
  46. RtWvOut, or RtDuplex classes). In many instances, a blocking scheme
  47. results in a clearer and more straight-forward program structure.
  48. Within a graphical user interface (GUI) programming context, however,
  49. callback routines are often more natural.
  50. In order to allow all STK programs to function with equal proficiency
  51. on all supported computer platforms, a decision was made to modify the
  52. example projects to use audio callback routines. The result is a more
  53. complicated code structure, which is unfortunate given that we
  54. generally strive to make STK code as clear as possible for educational
  55. purposes. This was especially an issue with the demo program because
  56. it is designed to function in both realtime and non-realtime contexts.
  57. The use of global variables has been avoided by defining data
  58. structures to hold all variables that must be accessible to the
  59. callback routine and other functions. Alternative schemes for making
  60. control updates could be designed depending on particular program
  61. needs and constraints.
  62. [<A HREF="tutorial.html">Main tutorial page</A>] &nbsp; [<A HREF="instruments.html">Next tutorial</A>]
  63. */