0

Heres the existing code, I already have. though it only retrieves one loudness and pitch value from the microphone

import aubio
import numpy as num
import pyaudio
import sys
import requests

BUFFER_SIZE             = 2048
CHANNELS                = 1
FORMAT                  = pyaudio.paFloat32
METHOD                  = "default"
SAMPLE_RATE             = 44100
HOP_SIZE                = BUFFER_SIZE//2
PERIOD_SIZE_IN_FRAME    = HOP_SIZE

def main(args):

    # Initiating PyAudio object.
    pA = pyaudio.PyAudio()
    # Open the microphone stream.
    mic = pA.open(format=FORMAT, channels=CHANNELS,
        rate=SAMPLE_RATE, input=True,
        frames_per_buffer=PERIOD_SIZE_IN_FRAME)

    # Initiating Aubio's pitch detection object.
    pDetection = aubio.pitch(METHOD, BUFFER_SIZE,
        HOP_SIZE, SAMPLE_RATE)
    # Set unit.
    pDetection.set_unit("Hz")
    # Frequency under -40 dB will considered
    # as a silence.
    pDetection.set_silence(-40)

    # Infinite loop!
    while True:

        # mic listening
        data = mic.read(PERIOD_SIZE_IN_FRAME)
        # Convert into number that Aubio understand.
        samples = num.fromstring(data,
            dtype=aubio.float_type)
        # Finally get the pitch.
        pitch = pDetection(samples)[0]
        # Compute the energy (volume)
        # of the current frame.
        volume = num.sum(samples**2)/len(samples)
        # format output
        # display 6 decimal digits like 0.000000.
        volume = "{:6f}".format(volume)

        # print loudness value and pitch value
        print(str(pitch/300) + " " + str(volume))
    url = "http://192.168.43.89:8000"
    requests.post(url, "[" + str(pitch/300) + "," + str(volume) + "]")
if __name__ == "__main__": main(sys.argv)

Goal: get the script to retrieve multiple samples of the microphone like 16 samples.

my psuedo code would look something like

    pitch1 = pDetection(samples)[0]
    pitch2 = pDetection(samples)[1]
    pitch3 = pDetection(samples)[2]
    pitch4 = pDetection(samples)[3]

   volume1 = num.sum(samples[0]**2)/len(samples[0])
   volume2 = num.sum(samples[1]**2)/len(samples[1])
   volume3 = num.sum(samples[2]**2)/len(samples[2])
   volume4 = num.sum(samples[3]**2)/len(samples[3])

And I think the output would be if i printed all the values:

0.00000
0.00000
0.00000
0.00000

0.00000
0.00000
0.00000
0.00000

These values have been made zeroes as if the microphone was silent and not being used

Lavacat
  • 11
  • 4
  • It's already in a loop, so it should be repeating it self endlessly. If you'd like to take multiple samples each loop, you've pretty much already written the solution to it, not quite sure what you're looking for here. – Vulpex Mar 06 '21 at 17:27
  • Tried using the psuedo-code, got the index out of range. – Lavacat Mar 07 '21 at 06:55

0 Answers0