User Tools

Site Tools


inputting_20real-time_20audio

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
inputting_20real-time_20audio [2024/04/19 16:05] – BBCSDL version added (tentative) richardrussellinputting_20real-time_20audio [2024/04/19 17:43] (current) richardrussell
Line 46: Line 46:
 You could of course present the choices and accept the selection in different ways, for example a dialogue box containing a list box plus OK and Cancel buttons.  Or you might prefer it to be determined by a configuration file rather than a user selection made each time the program is run.  That's entirely up to you; adapt the above code accordingly. You could of course present the choices and accept the selection in different ways, for example a dialogue box containing a list box plus OK and Cancel buttons.  Or you might prefer it to be determined by a configuration file rather than a user selection made each time the program is run.  That's entirely up to you; adapt the above code accordingly.
  
-==== Choosing the number and size of the buffers ====+==== Choosing the buffer size ====
  
-The first step is to decide how many audio buffers you need and how large they should be. To some extent this is an arbitrary decision, but it will depend on things like //latency// (how much time elapses between the audio arriving and it being processed by your program) and the amount of work needed to process the received audio data.\\ \\  Normally you should have at least three buffers: one inputting the sampled sound, one being processed by your program, and one spare (the buffers are reused cyclically). It is vitally important that your program can process the audio data quickly enough, otherwise data will be lost, with undesirable results. If there is any variability in the rate at which you can process the data (for example it depends on disk or network accesses) then you may need to use more and/or larger buffers to 'iron out' the fluctuation. Using more buffers is generally preferable to using larger buffers, to minimise any increase in latency.\\ \\  In the example below the number of buffers is three and the length of each buffer is 1024 samples; at 44100 Hz stereo that implies a latency of at least 24 milliseconds.+The first step is to decide how large the audio buffer should be. To some extent this is an arbitrary decision, but it will depend on things like //latency// (how much time elapses between the audio arriving and it being processed by your program) and the amount of work needed to process the received audio data.\\ \\  It is vitally important that your program can process the audio data quickly enough, otherwise data will be lost, with undesirable results. If there is any variability in the rate at which you can process the data (for example it depends on disk or network accesses) then you may need to use larger buffer to 'iron out' the fluctuation. In the example below the length of the buffer is 1024 samples; at 44100 Hz stereo that implies a latency of at least 24 milliseconds.
  
 <code bb4w> <code bb4w>
-        nBuffers% = 3 
         SamplesPerBuffer% = 1024         SamplesPerBuffer% = 1024
 </code> </code>
Line 71: Line 70:
 </code> </code>
  
-==== Opening the audio device and creating the buffers ====+==== Opening the audio device and creating the buffer ====
  
-Now the audio capture device can be opened and the buffers created:+Now the audio capture device can be opened and the buffer created:
  
 <code bb4w> <code bb4w>
Line 79: Line 78:
       IF Device% = 0 ERROR 100, "Couldn't open audio device " + deviceName$(index%)       IF Device% = 0 ERROR 100, "Couldn't open audio device " + deviceName$(index%)
  
-      SamplingRate% = have.freq% 
       BytesPerBuffer% = have.size%       BytesPerBuffer% = have.size%
       WordsPerBuffer% = BytesPerBuffer% DIV 4       WordsPerBuffer% = BytesPerBuffer% DIV 4
  
-      DIM Buffers{(nBuffers%-1) Data%(WordsPerBuffer% - 1)}+      DIM Buffer%(WordsPerBuffer% - 1)
 </code> </code>
  
Line 98: Line 96:
 ==== Inputting in real-time ==== ==== Inputting in real-time ====
  
-Once the above code has been executed you need to process the received audio buffers fast enough to keep up with the incoming data. The following code constantly checks whether any of the buffers needs processing and if so calls the **PROCprocessbuffer** routine:+Once the above code has been executed you need to process the received audio buffers fast enough to keep up with the incoming data. The following code constantly cycles, filling the buffer and calling the **PROCprocessbuffer** routine:
  
 <code bb4w> <code bb4w>
         REPEAT         REPEAT
-          FOR buff% = 0 TO nBuffers%-1 +          p%% = ^Buffer%(0) 
-            p%% = ^Buffers{(buff%)}.Data%(0) +          R% = BytesPerBuffer% 
-            R% = BytesPerBuffer% +          PROCprocessbuffer(p%%, SamplesPerBuffer%) 
-            PROCprocessbuffer(p%%, SamplesPerBuffer%) +          REPEAT 
-            REPEAT +            SYS "SDL_DequeueAudio", Device%, p%%, R%, @memhdc% TO I% 
-              SYS "SDL_DequeueAudio", Device%, p%%, R%, @memhdc% TO I% +            p%% += I% : R% -= I% 
-              p%% += I% : R% -= I% +          UNTIL R% <= 0
-            UNTIL R% <= 0 +
-          NEXT+
         UNTIL FALSE         UNTIL FALSE
 +        END
 </code> </code>
  
inputting_20real-time_20audio.1713542735.txt.gz · Last modified: 2024/04/19 16:05 by richardrussell