| 1 | <!doctype html public "-//w3c//dtd html 4.0 transitional//en"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> |
| 5 | <meta name="GENERATOR" content="Mozilla/4.77 [en]C-gatewaynet (Win98; U) [Netscape]"> |
| 6 | <meta name="Author" content="Phil Burk"> |
| 7 | <meta name="Description" content="Tutorial for PortAudio, a cross platform, open-source, audio I/O library.It provides a very simple API for recording and/or playing sound using a simple callback function."> |
| 8 | <meta name="KeyWords" content="audio, tutorial, library, portable, open-source, DirectSound,sound, music, JSyn, synthesis,"> |
| 9 | <title>PortAudio Tutorial</title> |
| 10 | </head> |
| 11 | <body> |
| 12 | |
| 13 | <center><table COLS=1 WIDTH="100%" BGCOLOR="#FADA7A" > |
| 14 | <tr> |
| 15 | <td> |
| 16 | <center> |
| 17 | <h1> |
| 18 | PortAudio Tutorial</h1></center> |
| 19 | </td> |
| 20 | </tr> |
| 21 | </table></center> |
| 22 | |
| 23 | <h2> |
| 24 | Blocking Read/Write Functions</h2> |
| 25 | |
| 26 | <blockquote>[Note: These functions are not part of the official PortAudio |
| 27 | API. They are simply built on top of PortAudio as an extra utility. Also |
| 28 | note that they are under evaluation and their definition may change.] |
| 29 | <p>There are two fundamentally different ways to design an audio API. One |
| 30 | is to use callback functions the way we have already shown. The callback |
| 31 | function operates under an interrupt or background thread This leaves the |
| 32 | foreground application free to do other things while the audio just runs |
| 33 | in the background. But this can sometimes be awkward. |
| 34 | <p>So we have provided an alternative technique that lets a program generate |
| 35 | audio in the foreground and then just write it to the audio stream as if |
| 36 | it was a file. If there is not enough room in the audio buffer for more |
| 37 | data, then the write function will just block until more room is available. |
| 38 | This can make it very easy to write an audio example. To use this tool, |
| 39 | you must add the files "pablio/pablio.c" and "pablio/ringbuffer.c" to your |
| 40 | project. You must also: |
| 41 | <blockquote> |
| 42 | <pre>#include "pablio.h"</pre> |
| 43 | </blockquote> |
| 44 | Here is a short excerpt of a program that opens a stream for input and |
| 45 | output. It then reads a block of samples from input, and writes them to |
| 46 | output, in a loop. The complete example can be found in "pablio/test_rw.c". |
| 47 | <blockquote> |
| 48 | <pre> #define SAMPLES_PER_FRAME (2) |
| 49 | #define FRAMES_PER_BLOCK (1024) |
| 50 | SAMPLE samples[SAMPLES_PER_FRAME * FRAMES_PER_BLOCK]; |
| 51 | PaError err; |
| 52 | PABLIO_Stream *aStream; |
| 53 | |
| 54 | /* Open simplified blocking I/O layer on top of PortAudio. */ |
| 55 | err = OpenAudioStream( &rwbl, SAMPLE_RATE, paFloat32, |
| 56 | (PABLIO_READ_WRITE | PABLIO_STEREO) ); |
| 57 | if( err != paNoError ) goto error; |
| 58 | |
| 59 | /* Process samples in the foreground. */ |
| 60 | for( i=0; i<(NUM_SECONDS * SAMPLE_RATE); i++ ) |
| 61 | { |
| 62 | /* Read one block of data into sample array from audio input. */ |
| 63 | ReadAudioStream( aStream, samples, FRAMES_PER_BLOCK ); |
| 64 | /* |
| 65 | ** At this point you could process the data in samples array, |
| 66 | ** and write the result back to the same samples array. |
| 67 | */ |
| 68 | /* Write that same frame of data to output. */ |
| 69 | WriteAudioStream( aStream, samples, FRAMES_PER_BLOCK ); |
| 70 | } |
| 71 | |
| 72 | CloseAudioStream( aStream );</pre> |
| 73 | </blockquote> |
| 74 | </blockquote> |
| 75 | <font size=+2><a href="http://www.portaudio.com/">home</a> | |
| 76 | <a href="pa_tutorial.html">contents</a> |
| 77 | | <a href="pa_tut_devs.html">previous</a> | <a href="pa_tut_explore.html">next</a></font> |
| 78 | </body> |
| 79 | </html> |