How to plotting the audio stream using input device(mic, USB type soundcard)?

990 views
Skip to first unread message

Hyeonjun Park

unread,
Apr 9, 2018, 5:26:31 AM4/9/18
to librosa
Hi all,

I'm tring to plotting the audio stream(real-time) using pyAudio with librosa.

I've searching in the Google, and I found some references:
    librosa reference: https://gist.github.com/mailletf/c49063d005dfc51a2df6
   
    pyAudio reference: https://gist.github.com/ZWMiller/53232427efc5088007cab6feee7c6e4c

So, I've tring to combine both code, like this:

import pyaudio
import numpy as np
import matplotlib.pyplot as plt
import librosa.display as libdis
import librosa

i
=0
f
,ax = plt.subplots(3)

# Prepare the Plotting Environment with random starting values
x
= np.arange(10000)
y
= np.random.randn(10000)

# Plot 0 is for raw audio data
li
, = ax[0].plot(x, y)
ax
[0].set_xlim(0,1000)
ax
[0].set_ylim(-5000,5000)
ax
[0].set_title("Raw Audio Signal")
# Plot 1 is for the FFT of the audio
li2
, = ax[1].plot(x, y)
ax
[1].set_xlim(0,5000)
ax
[1].set_ylim(-100,100)
ax
[1].set_title("Fast Fourier Transform")

# Plot 2 is for librosa test
li3
, = ax[2].plot(x, y)
ax
[2].set_xlim(0, 5000)
ax
[2].set_ylim(-5000,5000)
ax
[2].set_title("The librosa Test")

# Show the plot, but without blocking updates
plt
.pause(0.01)
plt
.tight_layout()


FORMAT
= pyaudio.paInt16 # We use 16bit format per sample
CHANNELS
= 1
RATE
= 44100
CHUNK
= 1024 # 1024bytes of data red from a buffer
RECORD_SECONDS
= 0.1
#WAVE_OUTPUT_FILENAME = "file.wav"

audio
= pyaudio.PyAudio()

# start Recording
stream
= audio.open(format=FORMAT,
 channels
=CHANNELS,
 rate
=RATE,
 input
=True)#,
 
#frames_per_buffer=CHUNK)

global keep_going
keep_going
= True

def plot_data(in_data):
 
# get and convert the data to float
 audio_data
= np.fromstring(in_data, np.int16)
 
# Fast Fourier Transform, 10*log10(abs) is to scale it to dB
 
# and make sure it's not imaginary
 dfft
= 10.*np.log10(abs(np.fft.rfft(audio_data)))
 
print(audio_data)

 
#librosa plotting test
 re_audio_data
= librosa.resample(audio_data, 44100, 22050)
 libdis
.waveplot(re_audio_data , sr=22050)

 li
.set_xdata(np.arange(len(audio_data)))
 li
.set_ydata(audio_data)
 li2
.set_xdata(np.arange(len(dfft))*10.)
 li2
.set_ydata(dfft)
 li3
.set_xdata(np.arange(len(audio_data)))
 li3
.set_ydata(audio_data)
 
# Show the updated plot, but without blocking
 plt
.pause(0.01)
 
if keep_going:
 
return True
 
else:
 
return False

# Open the connection and start streaming the data
stream
.start_stream()
print("\n+---------------------------------+")
print("Press Ctrl+C to Break Recording |")
print("+---------------------------------+\n")

# Loop so program doesn't end while the stream callback's
# itself for new data
while keep_going:
 
try:
 plot_data
(stream.read(CHUNK))
 
except KeyboardInterrupt:
 keep_going
=False
 
except:
 
pass

# Close up shop (currently not used because KeyboardInterrupt
# is the only way to close)
stream
.stop_stream()
stream
.close()

audio
.terminate()

If I run the code, then stop the plot and notting work.
I think, the line 64, this point is make a problem....

What wrong in this code?

And How do I fix it?
Please help me.

Thank you.


Reply all
Reply to author
Forward
0 new messages