Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aubio with audio file in realtime from python? #365

Open
minwook96 opened this issue May 26, 2022 · 0 comments
Open

Aubio with audio file in realtime from python? #365

minwook96 opened this issue May 26, 2022 · 0 comments

Comments

@minwook96
Copy link

minwook96 commented May 26, 2022

I want to know the pitch by frame by running the mp3 or wav file
When the pitch value is output, 0 is continuously displayed.
where is the problem?
I want to (Ex, C1, F2)

import aubio
import numpy as np
import pyaudio
import time
import argparse
import queue
import wave
import music21  # yes! new favorite library

# parser = argparse.ArgumentParser()
# parser.add_argument("-input", required=False, type=int, help="Audio Input Device")
# args = parser.parse_args()
#
# if not args.input:
#     print("No input device specified. Printing list of input devices now: ")
#     p = pyaudio.PyAudio()
#     for i in range(p.get_device_count()):
#         print("Device number (%i): %s" % (i, p.get_device_info_by_index(i).get('name')))
#     print("Run this program with -input 1, or the number of the input you'd like to use.")
#     exit()
CHUNK = 1024
wf = wave.open("temp.wav", 'rb')

# PyAudio object.
p = pyaudio.PyAudio()

# Open stream.
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                channels=wf.getnchannels(),
                rate=wf.getframerate(),
                output=True)
# stream = p.open(format=pyaudio.paFloat32,
#                 channels=1, rate=44100, input=True,
#                 input_device_index=args.input, frames_per_buffer=4096)
time.sleep(1)
data = wf.readframes(CHUNK)
# Aubio's pitch detection.
pDetection = aubio.pitch("default", 2048, 2048 // 2, 44100)
# Set unit.
pDetection.set_unit("Hz")
pDetection.set_silence(-40)

q = queue.Queue()


def get_current_note(volume_thresh=0.01, printOut=False):
    global data
    """Returns the Note Currently Played on the q object when audio is present

    Keyword arguments:

    volume_thresh -- the volume threshold for input. defaults to 0.01
    printOut -- whether or not to print to the terminal. defaults to False
    """
    current_pitch = music21.pitch.Pitch()

    while True:
        stream.write(data)
        data = wf.readframes(CHUNK)
        samples = np.fromstring(data, dtype=aubio.float_type)
        pitch = pDetection(samples)[0]
        print(pitch)
        # Compute the energy (volume) of the
        # current frame.
        volume = np.sum(samples ** 2) / len(samples) * 100
        # print(volume)
        if pitch > volume_thresh:  # adjust with your mic!
            current_pitch.frequency = pitch
        else:
            continue

        if printOut:
            print(current_pitch)

        else:
            current = current_pitch.nameWithOctave
            q.put({'Note': current, 'Cents': current_pitch.microtone.cents})


if __name__ == '__main__':
    get_current_note(volume_thresh=0.001, printOut=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant