outputting_20real-time_20audio
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
outputting_20real-time_20audio [2018/04/15 12:32] – Added syntax highlighting richardrussell | outputting_20real-time_20audio [2024/04/19 17:40] (current) – tentative richardrussell | ||
---|---|---|---|
Line 1: | Line 1: | ||
=====Outputting real-time audio===== | =====Outputting real-time audio===== | ||
+ | |||
+ | =====BBC BASIC for SDL 2.0===== | ||
+ | |||
+ | //by Richard Russell, April 2024// | ||
+ | |||
+ | //BBC BASIC for SDL 2.0// provides a number of methods for outputting sounds and music, for example the [[http:// | ||
+ | |||
+ | One way you can go about that is to create a **.WAV** or **.MP3** file containing the required audio content, and play that using the [[https:// | ||
+ | |||
+ | ==== Preliminaries ==== | ||
+ | |||
+ | As is usual for programs accessing the SDL 2.0 API, it is important to trap errors, and closing the window, so that the necessary ' | ||
+ | |||
+ | <code bb4w> | ||
+ | ON ERROR PROCcleanup : MODE 7 : PRINT REPORT$ : QUIT | ||
+ | ON CLOSE PROCcleanup : QUIT | ||
+ | </ | ||
+ | |||
+ | The **PROCcleanup** routine is listed later. You may want to change the error reporting to a different method. | ||
+ | |||
+ | ==== Selecting the audio format ==== | ||
+ | |||
+ | The first step is to decide the audio //format// you will use: the principal choices being of sampling rate (the main ones being 11025 Hz, 22050 Hz and 44100 Hz) and number of channels (mono, 1 channel, or stereo, 2 channels). The higher the sampling rate the higher the audio frequency that can be generated, but the more work your software needs to do. Normally you should choose the lowest sampling rate suitable for your application, | ||
+ | |||
+ | ==== Selecting the audio format ==== | ||
+ | |||
+ | The next step is to decide the audio //format// you will use: the principal choices being of sampling rate (the main ones being 11025 Hz, 22050 Hz and 44100 Hz) and number of channels (mono, 1 channel, or stereo, 2 channels). The higher the sampling rate the higher the audio frequency that can be received, but the more work your software needs to do. Normally you should choose the lowest sampling rate suitable for your application, | ||
+ | |||
+ | You set up the required audio format and open the audio device as follows: | ||
+ | |||
+ | <code bb4w> | ||
+ | DIM audiospec{freq%, | ||
+ | audiospec.freq% = 44100 | ||
+ | audiospec.format.h& | ||
+ | audiospec.format.l& | ||
+ | audiospec.channels& | ||
+ | |||
+ | SYS " | ||
+ | IF @hwo% = 0 ERROR 100, " | ||
+ | </ | ||
+ | |||
+ | In this example a sampling rate of 44100 Hz and monaural (single channel) output has been selected. | ||
+ | |||
+ | ==== Creating and initialising the buffer ==== | ||
+ | |||
+ | The next step is to decide how large the audio buffer needs to be. To some extent this is an arbitrary decision, but it will depend on things like //latency// (how much time elapses between your program generating the audio data and the actual sound output) and the amount of work needed to create the audio data.\\ \\ It is vitally important that your program can create the audio data quickly enough, otherwise there will be interruptions (clicks and stutters) to the sound output. If there is any variability in the rate at which you can generate the data (for example it depends on disk or network accesses) then you may need to use a larger buffer to 'iron out' the fluctuation, | ||
+ | |||
+ | <code bb4w> | ||
+ | SamplesPerBuffer% = 1024 | ||
+ | BytesPerBuffer% = SamplesPerBuffer% * audiospec.channels& | ||
+ | WordsPerBuffer% = BytesPerBuffer% DIV 4 | ||
+ | |||
+ | DIM Buffer%(WordsPerBuffer%) | ||
+ | </ | ||
+ | |||
+ | Note that in this case the audio buffer is allocated from BASIC' | ||
+ | |||
+ | ==== Outputting in real-time ==== | ||
+ | |||
+ | Once the above code has been executed you need to refill the audio buffer fast enough to keep up with the requested sampling rate. The following code cycles, calling the **PROCfillbuffer** routine then outputting the data: | ||
+ | |||
+ | <code bb4w> | ||
+ | REPEAT | ||
+ | lpData%% = ^Buffer%(0) | ||
+ | PROCfillbuffer(lpData%%, | ||
+ | SYS " | ||
+ | SYS " | ||
+ | REPEAT | ||
+ | WAIT 1 | ||
+ | SYS " | ||
+ | UNTIL S% < 2 * BytesPerBuffer% | ||
+ | UNTIL FALSE | ||
+ | END | ||
+ | </ | ||
+ | |||
+ | In this example the sound generation continues indefinitely, | ||
+ | |||
+ | ==== Generating the audio data ==== | ||
+ | |||
+ | Obviously it's only possible to describe this aspect in general terms, because precisely what audio data you need to generate will depend on what the program is designed to do. One of the simplest applications is to generate a single pure (sine wave) tone of a specified frequency. The code below does that, where the frequency (in Hz) is assumed to be in the global variable **Frequency**: | ||
+ | |||
+ | <code bb4w> | ||
+ | DEF PROCfillbuffer(B%%, | ||
+ | LOCAL I%, D | ||
+ | PRIVATE P | ||
+ | D = Frequency / audiospec.freq% * 2*PI : REM Phase change per sample | ||
+ | FOR I% = 0 TO 2*N%-2 STEP 2 | ||
+ | B%%!I% = 32767*SIN(P) | ||
+ | P += D | ||
+ | NEXT | ||
+ | ENDPROC | ||
+ | </ | ||
+ | |||
+ | This code is appropriate for monaural output (one channel) where each audio sample consists of a signed 16-bit value in the range -32767 to +32767. Note that, since BBC BASIC has no 16-bit operations, 32-bit indirection (" | ||
+ | |||
+ | ==== Cleaning up ==== | ||
+ | |||
+ | When you stop the sound generation, or exit the program, you need to shut down the audio output in a controlled fashion: | ||
+ | |||
+ | <code bb4w> | ||
+ | DEF PROCcleanup | ||
+ | IF @hwo% THEN | ||
+ | SYS " | ||
+ | SYS " | ||
+ | ENDIF | ||
+ | ENDPROC | ||
+ | </ | ||
+ | |||
+ | This code might form part of a larger routine, if there are other things that need to be shut down. | ||
+ | |||
+ | =====BBC BASIC for Windows===== | ||
//by Richard Russell, November 2006//\\ \\ BBC BASIC for Windows provides a number of methods for outputting sounds and music, for example the [[http:// | //by Richard Russell, November 2006//\\ \\ BBC BASIC for Windows provides a number of methods for outputting sounds and music, for example the [[http:// |
outputting_20real-time_20audio.1523795550.txt.gz · Last modified: 2024/01/05 00:17 (external edit)