def detectPitch(self): length, data = self.inp.read() samples = np.fromstring(data, dtype=np.int16) pitch = analyse.detect_pitch(samples) time.sleep(0.04) if pitch is not None: return pitch else: return 0
def processAudio(self, block): try: samples = fromstring(block, dtype=int16) freq = analyse.detect_pitch(samples) amplitude = self.getAmplitude(block) * 1000 self.detectedNoise = type(freq) != type(None) if (self.detectedNoise and amplitude >= self.amplitudeThreshold): self.pitch = Pitch(freq) else: self.pitch = None except: # Not best practices, but good for failing silently if PyAudio # feeds you garbage audio data (which happens) pass
def processAudio(self, block): try: samples = fromstring(block, dtype=int16) freq = analyse.detect_pitch(samples) amplitude = self.getAmplitude(block) * 1000 self.detectedNoise = type(freq) != type(None) if self.detectedNoise and amplitude >= self.amplitudeThreshold: self.pitch = Pitch(freq) else: self.pitch = None except: # Not best practices, but good for failing silently if PyAudio # feeds you garbage audio data (which happens) pass
def start_stream (): """This function should be executed in a separated thread because of infinite loop""" inp = alsaaudio.PCM(type= alsaaudio.PCM_CAPTURE, mode=alsaaudio.PCM_NORMAL,device='default') inp.setchannels(1) inp.setrate(44100) inp.setformat(alsaaudio.PCM_FORMAT_S16_LE) inp.setperiodsize(3072) while True: length, data = inp.read() samps = numpy.fromstring(data, dtype='int16') pitch = analyse.musical_detect_pitch(samps) hz = analyse.detect_pitch(samps) #CallAfter is necessary for making GUI method calls from non-GUI threads wx.CallAfter(show_everything,pitch,hz) # pitch is passed as an argument
def analyseChunk(CHUNK): """ analyseChunk(CHUNK): Cette fonction détermine le volume et la hauteur de la trame CHUNK. INPUT = Trame d'échantillions (np.array) OUTPUT = list(volume,pitch) See Nathan Whitehead's GitHub for further details on the <analyse> module. "https://github.com/ExCiteS/SLMPi/tree/master/SoundAnalyse-0.1.1" """ global prev_pitch volume = analyse.loudness(CHUNK) # Calcul du Volume pitch = analyse.detect_pitch(CHUNK) # Calcul de la hauteur if pitch == None: # Structure conditionnelle de fin pitch = prev_pitch else: prev_pitch = pitch return [volume, pitch]
def record(stop, lock): CHUNK = 1024 FORMAT = pyaudio.paInt16 CHANNELS = 2 RATE = 44100 WAVE_OUTPUT_FILENAME = 'output.wav' p = pyaudio.PyAudio() stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK) print("* recording") wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb') wf.setnchannels(CHANNELS) wf.setsampwidth(p.get_sample_size(FORMAT)) wf.setframerate(RATE) while(True): data = stream.read(CHUNK) a = numpy.fromstring(data, dtype='int16') print (analyse.detect_pitch(a, min_frequency=60.0, max_frequency=1500.0)) # print (numpy.abs(a).mean()) wf.writeframes(data) if stop.value > 0: lock.acquire() break print("* done recording") stream.stop_stream() stream.close() wf.close() p.terminate() lock.release()
def postProcess(sampsArray): frequencies = [] for samp in sampsArray: frequencies.append(analyse.detect_pitch(samp)) return frequencies
import numpy import pyaudio import analyse # Initialize PyAudio pyaud = pyaudio.PyAudio() # Open input stream, 16-bit mono at 44100 Hz # On my system, device 2 is a USB microphone, your number may differ. stream = pyaud.open( format=pyaudio.paInt16, channels=1, rate=44100, input_device_index=0, input=True # Initialize PyAudio ) while True: # Read raw microphone data rawsamps = stream.read(1024) # Convert raw data to NumPy array samps = numpy.fromstring(rawsamps, dtype=numpy.int16) # Show the volume and pitch print analyse.loudness(samps), analyse.detect_pitch(samps)
try: stream = pyaud.open( format = pyaudio.paInt16, channels = 1, rate = 44100, input_device_index = int(Microfone), input = True) except: print ("Falha ao Encontrar Dispositivo") sys.exit() Frequencia = "" while True: try: pegaentrada = stream.read(1024) except: print ("%.0f Hz" %Frequencia) os.system("cls") dados = numpy.fromstring(pegaentrada, dtype=numpy.int16) if analyse.detect_pitch(dados): print ("%.0f Hz" %analyse.detect_pitch(dados)) os.system("cls") Frequencia = analyse.detect_pitch(dados) else: print ("%.0f Hz" %Frequencia) os.system("cls")