Last active
November 3, 2024 03:49
-
-
Save drscotthawley/7c0a8b0f95d2c101ea3392ed6af85a63 to your computer and use it in GitHub Desktop.
Realtime oscilloscope in 20 lines of Python, via soundcard & OpenCV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@CrinSoft@ROmania-2023 - Python3: osc10l.py - DefaultMicrophone Oscilloscope in less than 10 lines of simple & clear Python code
