What is an audio block?

Viewed 132

I've seen some sentences including a word 'block'.

Pd lightens its workload by working with samples in blocks rather than individually. This greatly improves performance. The standard block size is 64 samples, but this setting can be changed. (http://pd-tutorial.com/english/ch03.html)

Rendering an audio graph is done in blocks of 128 samples-frames. A block of 128 samples-frames is called a render quantum, and the render quantum size is 128. (https://www.w3.org/TR/webaudio/#rendering-loop)

So, what I wonder are:

(1) What is wrong with handling samples individually? Why audio samples are grouped by a block of a some size (64, 128) ?

(2) Why the block size is a power of 2? // 2^6 = 64, 2^7 = 128

(3) After grouped, to where the samples go? Are they then played by a sound card or something?

2 Answers
  1. Go to the cashier, purchase a french fry, eat it, and repeat until you're full. That's the problem with processing samples one at a time -- there's a significant fixed cost to processing each bunch of samples, that doesn't depend on how big each bunch is. Bigger bunches => fewer bunches => less cost. (the tradeoff is delay)

  2. In computer systems, most block sizes are powers of 2 no matter what they are blocks of or what they're for. Historically that has been because it was more efficient to do math on powers of 2 using bit shifts instead of divisions. Especially for audio, though, there are transforms like the FFT that are commonly used in processing that are only conveniently implemented on blocks with power-of-2 sizes.

  3. Lots of stuff can happen after you process a block. Eventually, if the sound is being played, each block will be sent to the audio device driver, which will somehow arrange for it to be streamed out to the speakers or headphones one sample at a time.

A audio block is an array of floating point numbers representing audio. Where the numbers range from [1, -1] and where 0 (not 0.xxxx) is no sound.

1. What is wrong with handling samples individually? Why audio samples are grouped by a block of a some size (64, 128) ?

From my understanding handling samples in frames is better because of how web audio api runs in the browser due to performance. Different frame sizes can vary on the users performance on their PC. This is similar to fps on a video. In web audio there is node called ScriptProcesssorNode which allows you to create custom audio processing in a event handler at specific buffer/frame size. However the buffer size only ranged from 256 - 16384 or undefined for system preferred. It's simple on each function call it contains a new frame of audio. Basically it's a loop.

Now these days the ScriptProcessorNode is now deprecated/obsolete because of bad performance. This is because of the event handler onaudioprocess function is on the main thread which can block a lot of things. This is replaced with a AudioWorklet which is similar to the ScriptProcessorNode but each process call is only 128 frames and save better performance on the main thread because the AudioWorklet runs on the worker thread in background.

2. Why the block size is a power of 2? // 2^6 = 64, 2^7 = 128

I honestly don't exactly know but my best guess is that it is easier to calculate since in 8 bit alike numbers such as 8, 16, 32, 64, 128, 256 etc.

3. After grouped, to where the samples go? Are they then played by a sound card or something?

Web audio api is node graph system where sample frames (a.k.a 128 frame block) is passed through different nodes such as effects like BiquadFilterNode or custom processing blocks like AudioWorkletNode, etc. There is a node called AudioDestinationNode which is the speaker hardware output of the users PC. Any connection is connected to this particular node, (if there is sound), It will produce sound from that connection. Think of these nodes as a connect the dots with point A is the start and point B is the end. Each dot is a processing block like the AudioWorklet, etc, where point A is the source like a mic or a mp3/wav file and point B is the speaker destination. Connect them together, boom! You got amazing sounds.

I hope this makes sense and what your looking for :)
Feel free to correct me if have it wrong.

Related