This commit is contained in:
Correl Roush 2021-02-20 00:17:58 -05:00
parent c4acff7131
commit 122ea08f75
2 changed files with 58 additions and 0 deletions

View file

@ -1,4 +1,7 @@
#+title: Digital Audio Processing
Analysis and manipulation of [[file:20210219234904-pcm_audio.org][PCM Audio]].
* Sine Wave
#+name: sine-wave
#+begin_src python :exports code :results silent
@ -19,9 +22,47 @@
<<sine-wave>>
plt.plot(wave()[:100])
plt.title("Sine Wave")
plt.savefig("dap-sinewave.png")
return "dap-sinewave.png"
#+end_src
#+RESULTS:
[[file:dap-sinewave.png]]
* Getting volume with audioop
The power of an audio signal is computed using the *root mean square* of the
fragment:
\begin{equation}
{\displaystyle x_{\text{RMS}}={\sqrt {{\frac {1}{n}}\left(x_{1}^{2}+x_{2}^{2}+\cdots +x_{n}^{2}\right)}}.}
\end{equation}
#+begin_src python :noweb yes :results file
import audioop
import struct
from matplotlib import pyplot as plt
def sinewave():
<<sine-wave>>
wave = sinewave()
chunksize = 441
chunks = [wave[i : i + chunksize] for i in range(0, len(wave), chunksize)]
raw_chunks = [struct.pack(f"{chunksize}h", *map(int, chunk)) for chunk in chunks]
plt.plot(
[audioop.max(c, 2) for c in raw_chunks],
label="peak")
plt.plot(
[audioop.rms(c, 2) for c in raw_chunks],
label="rms")
plt.legend()
plt.title("Volume")
plt.savefig("dap-volume.png")
return "dap-volume.png"
#+end_src
#+RESULTS:
[[file:dap-volume.png]]

View file

@ -0,0 +1,17 @@
#+title: PCM Audio
Puse-code modulation digitally represents analog audio signals as a stream of
measurements of the amplitude of the signal sampled at uniform intervals (the
sampling rate) and quanitized to the nearest value within the range of available
steps (limited by the bit depth). Multiple audio channels are commonly
interleaved.
* Sampling Rate
Digital audio is typically sampled at 48 kHz (DVD video) or 44.1 kHz (CD audio).
* Bit Depth
| Bit Depth | Minimum Value | Maximum Value |
|-----------+---------------+---------------|
| 8 | -128 | 127 |
| 16 | -32768 | 32767 |
| 24 | -8388608 | 8388607 |