Browse Source

initial version

david 7 years ago
commit
ba69547940
3 changed files with 102 additions and 0 deletions
  1. 31 0
      play.py
  2. 29 0
      plot_wave.py
  3. 42 0
      realtime_audio_visualization.py

+ 31 - 0
play.py

@@ -0,0 +1,31 @@
+"""PyAudio Example: Play a WAVE file."""
+
+import pyaudio
+import wave
+import sys
+
+CHUNK = 1024
+
+if len(sys.argv) < 2:
+    print("Plays a wave file.\n\nUsage: %s filename.wav" % sys.argv[0])
+    sys.exit(-1)
+
+wf = wave.open(sys.argv[1], 'rb')
+
+p = pyaudio.PyAudio()
+
+stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
+                channels=wf.getnchannels(),
+                rate=wf.getframerate(),
+                output=True)
+
+data = wf.readframes(CHUNK)
+
+while data != '':
+    stream.write(data)
+    data = wf.readframes(CHUNK)
+
+stream.stop_stream()
+stream.close()
+
+p.terminate()

+ 29 - 0
plot_wave.py

@@ -0,0 +1,29 @@
+# Visualize wav file by matplotlib
+# Load the required libraries:
+#   * scipy
+#   * numpy
+#   * matplotlib
+from scipy.io import wavfile
+from matplotlib import pyplot as plt
+import sys
+import numpy as np
+
+if len(sys.argv) < 2:
+    print("Plays a wave file.\n\nUsage: %s filename.wav" % sys.argv[0])
+    sys.exit(-1)
+
+# Load the data and calculate the time of each sample
+samplerate, data = wavfile.read(sys.argv[1])
+times = np.arange(len(data))/float(samplerate)
+
+# Make the plot
+# You can tweak the figsize (width, height) in inches
+plt.figure(figsize=(30, 4))
+plt.fill_between(times, data, color='k')
+plt.xlim(times[0], times[-1])
+plt.xlabel('time (s)')
+plt.ylabel('amplitude')
+# You can set the format by changing the extension
+# like .pdf, .svg, .eps
+plt.savefig('plot.png', dpi=100)
+plt.show()

+ 42 - 0
realtime_audio_visualization.py

@@ -0,0 +1,42 @@
+import pyaudio
+import numpy as np
+import matplotlib.pyplot as plt
+import pyaudio
+import wave
+import sys
+
+CHUNK = 1024
+
+np.set_printoptions(suppress=True) # don't use scientific notation
+
+CHUNK = 4096 # number of data points to read at a time
+RATE = 44100 # time resolution of the recording device (Hz)
+
+
+p=pyaudio.PyAudio() # start the PyAudio class
+stream=p.open(format=pyaudio.paInt16,channels=1,rate=RATE,input=True,
+              frames_per_buffer=CHUNK) #uses default input device
+
+# create a numpy array holding a single read of audio data
+
+for i in range(300): #to it a few times just to see
+    data = np.fromstring(stream.read(CHUNK),dtype=np.int16)
+    data = data * np.hanning(len(data)) # smooth the FFT by windowing data
+    fft = abs(np.fft.fft(data).real)
+    fft = fft[:int(len(fft)/2)] # keep only first half
+    freq = np.fft.fftfreq(CHUNK,1.0/RATE)
+    freq = freq[:int(len(freq)/2)] # keep only first half
+    freqPeak = freq[np.where(fft==np.max(fft))[0][0]]+1
+    print("peak frequency: %d Hz"%freqPeak)
+
+    # uncomment this if you want to see what the freq vs FFT looks like
+    plt.plot(freq,fft)
+    plt.axis([0,4000,None,None])
+    plt.show()
+    plt.close()
+
+
+# close the stream gracefully
+stream.stop_stream()
+stream.close()
+p.terminate()