Skip to content

Instantly share code, notes, and snippets.

@drscotthawley
Last active May 3, 2023 19:02
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drscotthawley/7c0a8b0f95d2c101ea3392ed6af85a63 to your computer and use it in GitHub Desktop.
Save drscotthawley/7c0a8b0f95d2c101ea3392ed6af85a63 to your computer and use it in GitHub Desktop.
Realtime oscilloscope in 20 lines of Python, via soundcard & OpenCV
import numpy as np
import cv2
import soundcard as sc # Get it from https://github.com/bastibe/SoundCard
imWidth, imHeight = 1024, 512 # screen size
def draw_wave(screen, mono_audio, xs, title="oscilloscope", gain=5):
screen *= 0 # clear the screen
ys = imHeight/2*(1 - np.clip( gain * mono_audio[0:len(xs)], -1, 1)) # the y-values of the waveform
pts = np.array(list(zip(xs,ys))).astype(np.int) # pair up xs & ys
cv2.polylines(screen,[pts],False,(0,255,0)) # connect points w/ lines
cv2.imshow(title, screen) # show what we've got
default_mic = sc.default_microphone()
screen = np.zeros((imHeight,imWidth,3), dtype=np.uint8) # 3=color channels
xs = np.arange(imWidth).astype(np.int) # x values of pixels
while (1): # keep looping until someone stops this
with default_mic.recorder(samplerate=44100) as mic:
audio_data = mic.record(numframes=1024) # get some audio from the mic
draw_wave(screen, audio_data[:,0], xs) # draw left channel
key = cv2.waitKey(1) & 0xFF # keyboard input
if ord('q') == key: # quit key
break
@drscotthawley
Copy link
Author

The minus sign in the "1 - np.clip..." on line 7 is because in computer graphics, "up" is negative.

@drscotthawley
Copy link
Author

drscotthawley commented Jan 14, 2021

Note: Works on Mac, but soundcard lib yields a failed assertion on Linux / pulse audio: bastibe/SoundCard#29

@drscotthawley
Copy link
Author

I have a longer version with more controls for scaling, triggering adjustment, etc, but not in "20 lines" ;-)

@scarsi99
Copy link

Can I ask you your version, maybe if you can set a trigger level and having the trigger point ceneter, i mean, with a given pre-trigger?

@crin9
Copy link

crin9 commented May 2, 2023

@CrinSoft@ROmania-2023 - Python3: osc10l.py - DefaultMicrophone Oscilloscope in less than 10 lines of simple & clear Python code

@CrinSoft@ROmania-2023 - Python3: osc10l.py -#RO: OsciloscopeMicrofonImplicit in mai putin de 10 linii de cod Python simplu si clar

1 import sounddevice as sd # import SoundDevice lib (install lib with cmd: pip install sounddevice) #RO: importa librarie
2 import matplotlib.pyplot as plt # import PyPlot lib #RO: importa librarie
3 while True: # Ctrl+C in start window to stop this script! #RO: Apasa Ctrl+C in fer. de start sa opresti scriptul!
4 rec = sd.rec(int(44100*0.01),44100,1) # collect data from def_mic #RO: colectare date de la microfonul implicit
5 plt.plot(rec) # plot data #RO: deseneaza grafic datele
6 plt.axis('off') # clear graphic axes #RO: sterge axele graficului
7 plt.pause(0.01) # wait 0.01 sec before clear plot #RO: asteapta 0.01 sec inainte sa stergi graficul
8 plt.clf() # clear plot #RO: sterge graficul

if default mic is disabled, this script will run into error! #RO: daca microfonul nu e disponibil scriptul va da eroare!

@crin9
Copy link

crin9 commented May 3, 2023

@CrinSoft@ROmania-2023 - Python3: osc10l.py - DefaultMicrophone Oscilloscope in less than 10 lines of simple & clear Python code
osc10l

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment