I've set up a python audio stream and fft using bits of code for an audio spectrum analyser https://github.com/markjay4k/Audio-Spectrum-Analyzer-in-Python/blob/master/audio%20spectrum_pt2_spectrum_analyzer.ipynb (I removed all the plotting code), I want to find the most prominent frequency from my fft.
import numpy as np
import pyaudio
import struct
from scipy.fftpack import fft
import sys
import time
class AudioStream(object):
def __init__(self):
# stream constants
self.CHUNK = 1024 * 2
self.FORMAT = pyaudio.paInt16
self.CHANNELS = 1
self.RATE = 44100
self.pause = False
# stream object
self.p = pyaudio.PyAudio()
self.stream = self.p.open(
format=self.FORMAT,
channels=self.CHANNELS,
rate=self.RATE,
input=True,
output=True,
frames_per_buffer=self.CHUNK,
)
self.start_recording()
def start_recording(self):
print('stream started')
while True:
#Get data from stream and unpack to data_int
data = self.stream.read(self.CHUNK)
data_int = struct.unpack(str(2 * self.CHUNK) + 'B', data)
# compute FFT
yf = fft(data_int)
# find the most prominent frequency from this fft
if __name__ == '__main__':
AudioStream()
Below is a screenshot of the output from the non-adapted audio spectrum analyzer on github, showing the value I want to get from the fft (most prominent frequency). In this case, the value is around 1555Hz.