Example #1
0
def create_pitch_alg(pitch_method="default", pitch_buffer_size=4*512,
                     pitch_hop_size=256, pitch_samplerate=44100,
                     pitch_tolerance=0.8, pitch_unit="midi"):
    pitch_alg = aubio.pitch(pitch_method, pitch_buffer_size, pitch_hop_size, pitch_samplerate)
    pitch_alg.set_tolerance(pitch_tolerance)
    pitch_alg.set_unit(pitch_unit)
    return pitch_alg
Example #2
0
def aubio_f0yin(y, sr, nwind=1024, hop=512,
                method='yin', tolerance=None):
    ''' Applies f0 detection to a numpy vector using aubio
    '''
    from aubio import pitch, fvec

    po = pitch(method, nwind, hop, sr)
    vs = fvec(nwind)

    if tolerance is not None:
        if tolerance > 0.0 and tolerance < 1.0:
            po.set_tolerance(tolerance)
        else:
            sys.stderr.write('Tolerance not set: Out of bounds\n')

    nsamples = y.shape[0]

    freq = []
    time = []
    conf = []

    for ii in xrange(0,nsamples-nwind, hop):
        thisy = y[ii:ii+nwind]
        vs[:] = thisy
        time.append(float(ii+nwind/2)/sr)
        freq.append(po(vs))
        conf.append(po.get_confidence())
    return np.array(freq).squeeze(), np.array(time), np.array(conf)
Example #3
0
def get_user_audio():
    '''
    Using pyaudio, this function takes in mic input and returns the respective midi pitch value.
    '''
    # instantiate PyAudio object
    p = pyaudio.PyAudio()

    # open mic stream
    mic = p.open(format=FORMAT,
                 channels=CHANNELS,
                 rate=RATE,
                 input=True,
                 frames_per_buffer=PERIOD_SIZE_IN_FRAME)

    # Initiating Aubio's pitch detection object.
    pDetection = aubio.pitch(METHOD, CHUNK, HOP_SIZE, RATE)
    # Set unit.
    pDetection.set_unit("Hz")
    # Frequency under 5 dB will considered
    # as a silence (8 dB is a C1 or midi#0)
    pDetection.set_silence(-40)

    data = mic.read(PERIOD_SIZE_IN_FRAME)
    # Convert into number that Aubio understand.
    samples = np.fromstring(data, dtype=aubio.float_type)
    # Finally get the pitch.
    pitch = pDetection(samples)[0]

    midi = freq2midi(pitch)

    mic.stop_stream()
    mic.close()
    p.terminate()

    return midi
    def _compute_pitch_contour(self, window):
        """
        Computes the pitch contour of the audio data, along with the confidence curve.
        """
        win_s = 4096  # fft size
        hop_s = 512  # hop size
        samplerate = 8000
        tolerance = 0.8
        pitch_o = pitch("yin", win_s, hop_s, samplerate)
        pitch_o.set_unit("midi")
        pitch_o.set_tolerance(tolerance)

        INT_TO_FLOAT = 1. / 32768.  #2^15 (15 not 16 because it's signed)

        # convert to an array of 32-bit floats in the range [-1,1]
        pitch_input = np.float32(window * INT_TO_FLOAT)

        pitch_contour = []
        confidence_curve = []

        index = 0
        while True:
            samples = pitch_input[index * hop_s:(index + 1) * hop_s]
            pitch_output = pitch_o(samples)[0]
            confidence = pitch_o.get_confidence()
            pitch_contour += [pitch_output]  # append to contour
            confidence_curve += [confidence]
            index += 1
            if (index + 1) * hop_s > len(pitch_input):
                break  # stop when there are no more frames

        if self.debug:
            print("Pitch contour has length {}.".format(len(pitch_contour)))
        return pitch_contour, confidence_curve
Example #5
0
    def detect_pitch(self, mouse, stream) -> None:
        """
        Detect pitch of input chunks

        :param mouse: mouse handler instance
        :param stream: pyaudio stream instance

        :returns: None
        """
        pitch_detect = aubio.pitch("default", 2048, 2048 // 2, 44100)

        pitch_detect.set_unit("Hz")
        pitch_detect.set_silence(-40)
        print("Started. Hit the notes..")

        while True:
            data = stream.read(1024, exception_on_overflow=False)
            samples = np.fromstring(data, dtype=aubio.float_type)
            pitch = pitch_detect(samples)[0]

            # Compute the energy (volume) of the current frame.
            volume = np.sum(samples**2) / len(samples)
            volume = "{:.6f}".format(volume)

            mouse.handle_mouse(pitch)
Example #6
0
def getPitch(filename):
    from aubio import pitch, source
    # buffer size (window size) - higher is good for lower frequencies
    win_s = 4096
    hop_s = 512
    s = source(filename)  # wav samplerate is 44.1kH
    tolerance = 0.8
    pitch_o = pitch("yin", win_s)
    pitch_o.set_unit("midi")
    pitch_o.set_tolerance(tolerance)
    pitches, confidences = [], []
    # total number of frames read
    total_frames = 0
    while True:
        samples, read = s()
        pitch = pitch_o(samples)[0]
        pitch = round(pitch, 3)
        confidence = pitch_o.get_confidence()
        if confidence < 0.60:
            pitch = 0
        pitches += [pitch]
        confidences += [confidence]
        total_frames += read
        if read < hop_s:
            break
    return pitches
Example #7
0
    def setup(self, channels=None, samplerate=None,
              blocksize=None, totalframes=None):
        # Frame parameters setup
        if self._blocksize_s:
            self.input_blocksize = nextpow2(self._blocksize_s * samplerate)
        else:
            self.input_blocksize = 2048

        if self._stepsize_s:
            self.input_stepsize = int(np.round(self._stepsize_s * samplerate))
        else:
            self.input_stepsize = self.input_blocksize / 2

        # Now that frames size metadata are properly set, we can do the set-up
        super(AubioPitch, self).setup(channels,
                                      samplerate,
                                      blocksize,
                                      totalframes)


        # Aubio Pitch set-up
        self.aubio_pitch = pitch(
            "default", self.input_blocksize, self.input_stepsize,
            samplerate)
        self.aubio_pitch.set_unit("freq")
        self.block_read = 0
        self.pitches = []
        self.pitch_confidences = []
Example #8
0
def read_pitch(filename, chunk):
    win_s = chunk # fft size
    hop_s = win_s # hop size

    samplerate = 0
    # if len( sys.argv ) > 2: samplerate = int(sys.argv[2])

    s = source(filename, samplerate, hop_s)
    samplerate = s.samplerate

    pitch_o = pitch("default", win_s, hop_s, samplerate)
    pitch_o.set_unit("freq")

    pitches = []
    # times = []

    # total number of frames read
    total_frames = 0
    while True:
        samples, read = s()
        cur_pitch = pitch_o(samples)[0]
        # print "%f %f" % (total_frames / float(samplerate), pitch)
        pitches.append(Pitch(cur_pitch, total_frames / float(samplerate)))
        total_frames += read
        if read < hop_s: break

    # print pitches
    if len(pitches) == 0: return None
    return pitches
Example #9
0
 def __init__(self,
              filename,
              title,
              unit='midi',
              method='yin',
              samplerate=12800,
              hop=512,
              win_s=4096):
     """
     Default values  128000 / 512 = 25 sample per second.
     """
     self.filename = filename
     self.title = title
     self.unit = unit
     self.hop = hop
     self.win_s = win_s
     # Support to frequency not completed and never used
     self.is_midi = self.unit == 'midi'
     self.method = method
     self.samplerate = samplerate
     self.s = source(filename, self.samplerate, self.hop)
     self.pitch_o = pitch(self.method, self.win_s, self.hop,
                          self.samplerate)
     self.pitch_o.set_unit(self.unit)
     self.pitch_o.set_silence(self.silence)
     self.pitch_o.set_tolerance(self.tolerance)
def getPitch(filename):
    from aubio import pitch
    downsample = 1
    samplerate = 44100 / downsample
    win_s = 1024 / downsample # fft size

    hop_s = 256/ downsample # hop size
    s = source(filename, samplerate, hop_s)
    samplerate = s.samplerate
    tolerance = 0.8

    pitch_o = pitch("yin", win_s, hop_s, samplerate)
    pitch_o.set_unit("midi")
    pitch_o.set_tolerance(tolerance)
    total_frames = 0
    pitches = []
    confidences=[]
    while True:
        samples, read = s()
        pitch = pitch_o(samples)[0]
        confidence = pitch_o.get_confidence()
        confidences+=[confidence]
        pitches += [pitch]
        total_frames += read
        if read < hop_s: break
    pitches = array(pitches[1:])
    confidences = array(confidences[1:])
    cleaned_pitches = pitches
    cleaned_pitches = ma.masked_where(confidences < tolerance, cleaned_pitches,copy=False)
    cleaned_pitches = cleaned_pitches[~cleaned_pitches.mask]

    return max(cleaned_pitches)
Example #11
0
def extract_pitch(sig, fs):
    """ Extract MIDI pitch of signal """
    downsample = 1
    samplerate =  fs

    win_s = 4096 / downsample # fft size
    hop_s = 512  / downsample # hop size

    tolerance = 0.8

    #['default', 'schmitt', 'fcomb', 'mcomb', 'yin', 'yinfft']
    pitch_o = pitch("default", win_s, hop_s, samplerate)
    pitch_o.set_unit("midi")
    pitch_o.set_tolerance(tolerance)

    pitches = []
    confidences = []

    # total number of frames read
    i = 0
    while i + hop_s < len(sig[:,0]):
        samples = sig[i:i+hop_s][:,0]
        my_pitch = pitch_o(samples)
        #pitch = int(round(pitch))
        confidence = pitch_o.get_confidence()
        #if confidence < 0.8: pitch = 0.
        #print "%f %f %f" % (total_frames / float(samplerate), pitch, confidence)
        pitches += [my_pitch]
        confidences += [confidences]
        i += hop_s

    pitches = np.array(pitches)
    pitches = pitches[pitches > 10]
    return np.median(pitches)    # we return the median of the frequencies found
Example #12
0
def pitches(filename, samplerate=None, downsample=1):
    if samplerate is None:
        samplerate = 44100 / downsample

    win_s = 4096 / downsample # fft size
    hop_s = 512  / downsample # hop size

    s = aubio.source(filename, samplerate, hop_s)
    samplerate = s.samplerate

    tolerance = 0.8

    pitch_o = aubio.pitch("yin", win_s, hop_s, samplerate)
    pitch_o.set_unit("midi")
    pitch_o.set_tolerance(tolerance)

    pitches = []
    confidences = []

# total number of frames read
    total_frames = 0
    while True:
        samples, read = s()
        pitch = pitch_o(samples)[0]
        #pitch = int(round(pitch))
        confidence = pitch_o.get_confidence()
        #if confidence < 0.8: pitch = 0.
        #print "%f %f %f" % (total_frames / float(samplerate), pitch, confidence)
        pitches += [pitch]
        confidences += [confidence]
        total_frames += read
        if read < hop_s: break

    return samples, pitches, confidences, total_frames
Example #13
0
def get_pitch(filename):
    downsample = 1
    samplerate = 44100 // downsample
    # if len( sys.argv ) > 2: samplerate = int(sys.argv[2])

    win_s = 4096 // downsample  # fft size
    hop_s = 512 // downsample  # hop size

    s = source(filename, samplerate, hop_s)
    samplerate = s.samplerate

    tolerance = 0.8

    pitch_o = pitch("schmitt", win_s, hop_s, samplerate)
    pitch_o.set_unit("Hz")
    pitch_o.set_tolerance(tolerance)

    pitches = []
    confidences = []

    # total number of frames read
    total_frames = 0
    while True:
        samples, read = s()
        p = pitch_o(samples)[0]
        # pitch = int(round(p))
        confidence = pitch_o.get_confidence()
        # if confidence < 0.8: p = 0.
        # print("%f %f %f" % (total_frames / float(samplerate), p, confidence))
        pitches += [p]
        confidences += [confidence]
        total_frames += read
        if read < hop_s:
            break
    return np.mean(pitches)
Example #14
0
def getAmp(filename):
    downsample = 8
    samplerate = 44100 // downsample
    win_s = 4096 // downsample  # fft size
    hop_s = 512 // downsample  # hop size
    s = aubio.source(filename, samplerate, hop_s)
    samplerate = s.samplerate
    tolerance = 0.8
    pitch_o = aubio.pitch("yin", win_s, hop_s, samplerate)
    pitch_o.set_unit("freq")
    pitch_o.set_tolerance(tolerance)
    pitches = []
    confidences = []
    # total number of frames read
    total_frames = 0
    counter = 0
    while True:
        samples, read = s()
        pitch = pitch_o(samples)[0]
        confidence = pitch_o.get_confidence()
        #print "%f %f %f" % (total_frames / float(samplerate), pitch, confidence)
        pitches += [pitch]
        confidences += [confidence]
        total_frames += read
        if read < hop_s: break
    letterList = []
    newPitches = []
    amplitudes = []
    for index in range(0, len(pitches)):
        amp = getSection(
            filename,
            index,
        )  #, index + 5)
        amplitudes.append(amp.rms)
    return amplitudes
Example #15
0
def getPitchDiff(path):
	downsample = 1
	samplerate = 44100 / downsample

	win_s = 4096 / downsample # fft size
	hop_s = 512  / downsample # hop size

	s = source(path, samplerate, hop_s)
	samplerate = s.samplerate

	tolerance = 0.8

	pitch_o = aubio.pitch("yin", win_s, hop_s, samplerate)
	pitch_o.set_unit("midi")
	pitch_o.set_tolerance(tolerance)

	pitches = []
	confidences = []

	# total number of frames read
	total_frames = 0
	while True:
	    samples, read = s()
	    pitch = pitch_o(samples)[0]
	    #pitch = int(round(pitch))
	    confidence = pitch_o.get_confidence()
	    #if confidence < 0.8: pitch = 0.
	    #print "%f %f %f" % (total_frames / float(samplerate), pitch, confidence)
	    pitches += [pitch]
	    confidences += [confidence]
	    total_frames += read
	    if read < hop_s: break
	return np.median(np.fabs(np.diff(pitches)))
Example #16
0
def harmonize(name):
  # load data
  # y = AudioSegment.from_file(name+'.m4a', 'm4a')
  # y.export(name+'.wav', format='wav')
  y, sr = librosa.load(name)
  label = get_label(name, sr)
  # create pitch object (for pitch detection)
  p = aubio.pitch("default", samplerate=sr)
  # pad end of input vector with zeros
  pad_length = p.hop_size - y.shape[0] % p.hop_size
  y_padded = np.pad(y, (0, pad_length), 'constant', constant_values=0)
  # to reshape it in blocks of hop_size
  y_padded = y_padded.reshape(-1, p.hop_size)
  # input array should be of type aubio.float_type (defaults to float32)
  y_padded = y_padded.astype(aubio.float_type)
  # get sample index and note from original song
  idx_and_notes = get_idx_and_note(y_padded, p)
  for (i, n) in idx_and_notes:
    print(i/sr, n)
  # get mixed data
  mixed_data = mix_data(idx_and_notes, label, sr)
  # get corresponding chord ratios from chord label
  ratios = [(idx, get_target_keys(note, lab))
            for (idx, note, lab) in mixed_data]
  # get chord data
  chord_data = get_chord_data(y, sr, ratios)
  # write result
  librosa.output.write_wav(name.split(".")[0]+"_t.wav", chord_data, sr)
Example #17
0
def avg_frequence(path_to_music):
    downsample = 1
    samplerate = 44100 // downsample

    win_s = 4096 // downsample
    hop_s = 512 // downsample

    src = aubio.source(path_to_music, samplerate, hop_s)
    samplerate = src.samplerate

    pitch_o = aubio.pitch("yinfft", win_s, hop_s, samplerate)
    
    total_frames = 0
    counter = 0
    total_pitch = 0

    while True:
        counter += 1
        samples, read = src()

        pitch = pitch_o(samples)[0]

        total_frames += read
        total_pitch += pitch

        if ((total_frames / float(samplerate)) > 5.1):
            break

    return (total_pitch/counter)
Example #18
0
    def __init__(self, song_path):
        super(AudioController, self).__init__()
        self.audio = Audio(2, input_func=self.receive_audio)
        self.mixer = Mixer()
        self.audio.set_generator(self.mixer)
        self.song = song_path
        melody_path = self.song + '_melody.wav'
        harmony_path = self.song + '_harmony.wav'
        self.melody_track = WaveGenerator(WaveFile(melody_path))
        self.harmony_track = WaveGenerator(WaveFile(harmony_path))
        self.mixer.add(self.melody_track)
        self.mixer.add(self.harmony_track)
        self.melody_mute = False
        self.harmony_mute = False

        self.pitch = 0
        self.input_buffers = []
        self.downsample = 1
        self.samplerate = 44100 // self.downsample
        if len(sys.argv) > 2: self.samplerate = int(sys.argv[2])
        self.win_s = 4096 // self.downsample  # fft size
        self.hop_s = 512 // self.downsample  # hop size
        self.tolerance = 0.8
        self.pitch_o = pitch("yin", self.win_s, self.hop_s, self.samplerate)
        self.pitch_o.set_unit("midi")
        self.pitch_o.set_tolerance(self.tolerance)
Example #19
0
 def setup(self, channels=None, samplerate=None, blocksize=None, totalframes=None):
     super(AubioPitch, self).setup(channels, samplerate, blocksize, totalframes)
     self.aubio_pitch = pitch("default", self.input_blocksize, self.input_stepsize, samplerate)
     self.aubio_pitch.set_unit("freq")
     self.block_read = 0
     self.pitches = []
     self.pitch_confidences = []
Example #20
0
    def main(self, args):
        # Initiating PyAudio object.
        pA = pyaudio.PyAudio()
        # Open the microphone stream.
        mic = pA.open(format=self.FORMAT,
                      channels=self.CHANNELS,
                      rate=self.SAMPLE_RATE,
                      input=True,
                      frames_per_buffer=self.PERIOD_SIZE_IN_FRAME)

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

        while True:
            # Update microphone stream
            data = mic.read(self.PERIOD_SIZE_IN_FRAME)
            # Translate to aubio
            samples = num.fromstring(data, dtype=aubio.float_type)
            # Find pitch from samples
            pitch = pDetection(samples)[0]
            # Get volume from samples
            volume = num.sum(samples**2) / len(samples)
            # Format volume for 6 digit float
            detect_pitch = self.pitch_detection(pitch)

            # Finally print the pitch and the volume.
            print(
                str(pitch) + " " + str(volume) + " " +
                (str(self.current_color)))
Example #21
0
    def initializeAudio(self):
        global file
        # opens file as wave file
        self.src = file + ".wav"
        samplerate = 0
        self.total_frames = 0

        # initialize aubio data
        self.a_source = aubio.source(self.src, samplerate, self.hop_s)
        self.samplerate = self.a_source.samplerate
        self.p = pyaudio.PyAudio()
        self.format = pyaudio.paFloat32
        self.frames = self.hop_s
        self.channels = 1
        self.p = pyaudio.PyAudio()

        self.a_tempo = aubio.tempo("default", self.win_s, self.hop_s,
                                   self.samplerate)
        self.pitch_o = aubio.pitch("yin", self.win_s, self.hop_s,
                                   self.samplerate)
        self.notes_o = aubio.notes("default", self.win_s, self.hop_s,
                                   self.samplerate)
        self.o = aubio.onset("default", self.win_s, self.hop_s,
                             self.samplerate)
        self.o2 = aubio.onset("hfc", self.win_s, self.hop_s, self.samplerate)

        print("Audio set up for", file)
Example #22
0
def record():
    p = pyaudio.PyAudio()

    noteList = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]
    buffer_size = 1024
    pyaudio_format = pyaudio.paFloat32
    n_channels = 1
    samplerate = 44100
    stream = p.open(format=pyaudio_format,
                channels=n_channels,
                rate=samplerate,
                input=True,
                frames_per_buffer=buffer_size)
    tolerance = 0.8
    win_s = 4096 # fft size
    hop_s = buffer_size # hop size
    pitch_o = aubio.pitch("default", win_s, hop_s, samplerate)
    pitch_o.set_unit("Hz")
    pitch_o.set_tolerance(tolerance)
    audiobuffer = stream.read(buffer_size)
    signal = np.fromstring(audiobuffer, dtype=np.float32)

    pitch = pitch_o(signal)[0]
    confidence = pitch_o.get_confidence()
    #stream.stop_stream()
    #stream.close()
    #p.terminate()
    sungNote = hztoNote(pitch)
    # if(sungNote != None):
        # print(sungNote)
    return sungNote
Example #23
0
def read_pitch(filename, chunk):
    win_s = chunk  # fft size
    hop_s = win_s  # hop size

    samplerate = 0
    # if len( sys.argv ) > 2: samplerate = int(sys.argv[2])

    s = source(filename, samplerate, hop_s)
    samplerate = s.samplerate

    pitch_o = pitch("default", win_s, hop_s, samplerate)
    pitch_o.set_unit("freq")

    pitches = []
    # times = []

    # total number of frames read
    total_frames = 0
    while True:
        samples, read = s()
        cur_pitch = pitch_o(samples)[0]
        # print "%f %f" % (total_frames / float(samplerate), pitch)
        pitches.append(Pitch(cur_pitch, total_frames / float(samplerate)))
        total_frames += read
        if read < hop_s: break

    # print pitches
    if len(pitches) == 0: return None
    return pitches
Example #24
0
    def setup(self, channels=None, samplerate=None,
              blocksize=None, totalframes=None):
        super(AubioPitch, self).setup(channels,
                                      samplerate,
                                      blocksize,
                                      totalframes)


        # Frame parameters setup
        if self._blocksize_s:
            self.input_blocksize = nextpow2(self._blocksize_s * samplerate)
        else:
            self.input_blocksize = 2048

        if self._stepsize_s:
            self.input_stepsize = nextpow2(self._stepsize_s * samplerate)
        else:
            self.input_stepsize = self.input_blocksize / 2

        # Aubio Pitch set-up
        self.aubio_pitch = pitch(
            "default", self.input_blocksize, self.input_stepsize,
            samplerate)
        self.aubio_pitch.set_unit("freq")
        self.block_read = 0
        self.pitches = []
        self.pitch_confidences = []
    def __init__(self):
        """ Initialize entrainer for detecting pitch and tempo. """
        # Where is Praat? Assumes it has been added to your PATH.
        self.praat = "praat"
        # Where is the Praat script we will use for processing?
        self.script = "rr_entrain_speech.praat"

        # Define pitch ranges that will be considered speech.
        # - Human child speech: generally 250-400Hz.
        # - Human adult male speech: generally 85-180Hz.
        # - Human adult female speech: generally 165-255Hz.
        self._floor_pitch = 100
        self._ceiling_pitch = 600
        # For audio recording.
        self._buffer_size = 2048
        self._samplerate = 44100  #TODO Pass sample rate in as argument?
        self._n_channels = 1

        # Set up to detect pitch.
        _tolerance = 0.8
        _win_s = 4096  # fft size
        _hop_s = self._buffer_size  # hop size
        # Set up the aubio pitch detector.
        self.pitch_detector = aubio.pitch("default", _win_s, _hop_s,
                                          self._samplerate)
        self.pitch_detector.set_unit("Hz")
        self.pitch_detector.set_tolerance(_tolerance)
Example #26
0
def read_frequencies(filename):
    #we will use the original amount of samples in the file
    #samplerate = 44100
    s = source(filename)
    tolerance = 0.8

    #yin is a frequency estimation algorithm
    pitch_o = pitch("yin", samplerate=s.samplerate)
    pitch_o.set_unit("Hz")
    pitch_o.set_tolerance(tolerance)

    pitches = []
    confidences = []

    # total number of frames read
    total_frames = 0
    while True:
        samples, read = s()
        this_pitch = pitch_o(samples)[0]

        confidence = pitch_o.get_confidence()

        pitches += [this_pitch]
        confidences += [confidence]
        total_frames += read
        if read < s.hop_size: break

    #close that audio when we are done with it
    s.close()

    return pitches, confidences
Example #27
0
    def setup(self, channels=None, samplerate=None,
              blocksize=None, totalframes=None):
        super(AubioPitch, self).setup(channels,
                                      samplerate,
                                      blocksize,
                                      totalframes)
        # Frame parameters setup
        if self._blocksize_s:
            self.input_blocksize = nextpow2(self._blocksize_s)
        else:
            self.input_blocksize = 2048

        if self._stepsize_s:
            self.input_stepsize = nextpow2(self._stepsize_s)
        else:
            self.input_stepsize = nextpow2(self.input_blocksize / 2)

        self.result_blocksize = self.input_blocksize
        self.result_stepsize = self.input_stepsize
        self._PITCH_WINDOW = int(4096.0 / self.input_blocksize)
        self._BUFF_FRAMES = (self.process_pipe.processors[0].output_blocksize \
                             - (self.input_blocksize // 2)) // self.input_stepsize + 2
        
        # Aubio Pitch set-up
        self.aubio_pitch = pitch(
            "default", self.input_blocksize, self.input_stepsize,
            samplerate)
        self.aubio_pitch.set_unit("freq") #Hz?
        self.block_read = 0
        self.pitches = []
#        self.pitch_confidences = []
        self.pitch_stds = []
        self.monotone = 0
Example #28
0
def initializeMic(data):
    #Taken and modified from
    #https://gist.github.com/notalentgeek/48aeab398b6b74e3a9134a61b6b79a36

    #Initializing audio objects to be used in getting audio input and
    #determining pitch
    BUFFER_SIZE = 2048
    CHANNELS = 1
    FORMAT = pyaudio.paFloat32
    METHOD = "default"
    SAMPLE_RATE = 44100
    HOP_SIZE = BUFFER_SIZE // 2
    PERIOD_SIZE_IN_FRAME = HOP_SIZE  #2048 // 2

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

    # Initiating Aubio's pitch detection object.
    data.pDetection = aubio.pitch(METHOD, BUFFER_SIZE, HOP_SIZE, SAMPLE_RATE)
    # Set unit.
    data.pDetection.set_unit("Hz")
    # Frequency under -40 dB will considered
    # as a silence.
    data.pDetection.set_silence(-40)
Example #29
0
    def setup(self):
        self.filename = sys.argv[1]
        # self.filename = "middle_c.wav"
        self.mixer.add(WaveGenerator(WaveFile(self.filename)))
        # print "filename: ", self.filename
        self.downsample = 1
        self.samplerate = 44100 // self.downsample
        if len(sys.argv) > 2: self.samplerate = int(sys.argv[2])

        self.win_s = 4096 // self.downsample  # fft size
        self.hop_s = 512 // self.downsample  # hop size

        self.s = source(self.filename, self.samplerate, self.hop_s)
        print "s", self.s
        self.samplerate = self.s.samplerate

        self.tolerance = 0.8

        self.pitch_o = pitch("yin", self.win_s, self.hop_s, self.samplerate)
        self.pitch_o.set_unit("midi")
        self.pitch_o.set_tolerance(self.tolerance)

        self.pitches = []
        self.confidences = []

        # total number of frames read
        self.total_frames = 0
Example #30
0
def pitchreader(filename, samplerate):

    from aubio import source, pitch

    downsample = 1

    win_s = 4096 // downsample
    hop_s = 512 // downsample

    s = source(filename, int(samplerate), hop_s)
    samplerate = s.samplerate

    tolerance = 0.8

    pitch_o = pitch("yin", win_s, hop_s, samplerate)
    pitch_o.set_unit("midi")
    pitch_o.set_tolerance(tolerance)

    pitches = []
    confidences = []

    totalFrames = 0

    while True:
        samples, read = s()
        pitch = pitch_o(samples)[0]
        # pitch = int(round(pitch))
        confidence = pitch_o.get_confidence()
        currentFrame = totalFrames / float(samplerate)
        yield pitch  #can add back current frame if necessary
        pitches += [pitch]
        confidences += [confidence]
        totalFrames += read
        if read < hop_s: break
Example #31
0
    def _get_pitch(self, tolerance=0.8):
        def process_pitch(pitch_array):
            if self._bar_count == 0:
                return round(np.median(pitch_array))

            bar_pitch_len = int(np.floor(len(pitch_array) / self._bar_count))

            pitch_bars = []
            for j in range(0, bar_pitch_len * self._bar_count, bar_pitch_len):
                pitch_bars += [
                    round(np.median(pitch_array[j:j + bar_pitch_len]))
                ]

            return pitch_bars

        pitch_o = pitch("yinfft", self._win_size, self._hop_size,
                        self._samplerate)
        pitch_o.set_unit("Hz")
        pitch_o.set_tolerance(tolerance)

        if self._stereo:
            samples = self._get_samples(self._wav_array,
                                        'L+R').astype(np.float32)
        else:
            samples = self._get_samples(self._wav_array,
                                        'Mono').astype(np.float32)

        pitches = []
        steps = len(samples) - (len(samples) % self._hop_size)

        for i in range(0, steps, self._hop_size):
            pitchin = pitch_o(samples[i:i + self._hop_size])[0]
            pitches += [pitchin]

        return process_pitch(pitches)
Example #32
0
    def __init__(self, get_audio_over_ros):
        """ Initialize entrainer for detecting pitch and tempo. """
        # Where is Praat? Assumes it has been added to your PATH.
        self.praat = "praat"
        # Where is the Praat script we will use for processing?
        self.script = "rr_entrain_speech.praat"

        # These are the settings that the ROS android microphone node uses for
        # getting audio. These are defaults; we will update them after we get
        # the actual values from the audio messages.
        if get_audio_over_ros:
            self._buffer_size = 2048
            self.samplerate = 44100
            self.n_channels = 1
            self.sample_size = 2
        # Otherwise, we record audio from a local microphone.
        else:
            self._buffer_size = 2048
            self.samplerate = 44100
            self.n_channels = 1

        # Set up the aubio pitch detector.
        self.pitch_detector = aubio.pitch(
            "default",
            4096,  # FFT size
            self._buffer_size,  # hop size
            self.samplerate)
        self.pitch_detector.set_unit("Hz")
        self.pitch_detector.set_tolerance(0.8)
Example #33
0
def iter_samples_to_pitches(samples,
                            sample_rate=11025,
                            method='yin',
                            buf_size=2048,
                            hop_size=256,
                            yield_confidence=False):
    """Yield pitches.

    Parameters
    ----------
    samples : numpy.ndarray
        Samples in Numpy array.

    Yields
    ------
    pitch : float
        Pitch in Hertz
    confidence : float
        Confidence of the pitch detection.

    """
    pitcher = aubio.pitch(method, buf_size, hop_size, int(sample_rate))
    for sample_chunk in iter_chunk(samples):
        pitch = pitcher(sample_chunk.astype(np.float32))[0]
        if yield_confidence:
            confidence = pitcher.get_confidence()
            yield pitch, confidence
        else:
            yield pitch
Example #34
0
def getOnset(f):
    s = source(f, 0, hop_s)
    samplerate = s.samplerate

    tolerance = 0.8
    pitch_o = pitch("yin", win_s, hop_s, samplerate)
    pitch_o.set_unit("Hz")
    pitch_o.set_tolerance(tolerance)

    o = onset("default", win_s, hop_s, samplerate)
    notes_o = notes("default", win_s, hop_s, samplerate)

    # list of onsets, in seconds
    onsets = []
    vel = []
    pitches = []

    # total number of frames read
    total_frames = 0
    while True:
        samples, read = s()
        p = pitch_o(samples)[0]
        new_note = notes_o(samples)
        t = total_frames / float(samplerate)

        if o(samples) and p > 0:
            onsets.append(o.get_last_s())
            pitches.append(p)

        if new_note[0] != 0:
            vel.append(new_note[1])
        total_frames += read
        if read < hop_s:
            break
    return onsets, vel, pitches
Example #35
0
def get_pitch(filename, average_frequency):
    win_s = 4096
    hop_s = 512

    s = source(filename, samplerate, hop_s)
    samplerate = s.samplerate

    tolerance = 0.8

    pitch_o = pitch("yin", win_s, hop_s, samplerate)
    pitch_o.set_unit("midi")
    pitch_o.set_tolerance(tolerance)

    pitches = []
    confidences = []

    total_frames = 0
    while True:
        samples, read = s()
        pitch = pitch_o(samples)[0]
        pitches += [pitch]
        confidence = pitch_o.get_confidence()
        confidences += [confidence]
        total_frames += read
        if read < hop_s: break

    average_frequency = int(np.array(pitches).mean())
    return average_frequency
Example #36
0
def aubio_f0yin(y, sr, nwind=1024, hop=512, method='yin', tolerance=None):
    ''' Applies f0 detection to a numpy vector using aubio
    '''
    from aubio import pitch, fvec

    po = pitch(method, nwind, hop, sr)
    vs = fvec(nwind)

    if tolerance is not None:
        if tolerance > 0.0 and tolerance < 1.0:
            po.set_tolerance(tolerance)
        else:
            sys.stderr.write('Tolerance not set: Out of bounds\n')

    nsamples = y.shape[0]

    freq = []
    time = []
    conf = []

    for ii in xrange(0, nsamples - nwind, hop):
        thisy = y[ii:ii + nwind]
        vs[:] = thisy
        time.append(float(ii + nwind / 2) / sr)
        freq.append(po(vs))
        conf.append(po.get_confidence())
    return np.array(freq).squeeze(), np.array(time), np.array(conf)
def getPitch(filename):
	from aubio import source, pitch
	downsample = 1
	samplerate = RATE//downsample
	win_s = 4096 // downsample # fft size
	hop_s = 512  // downsample # hop size
	s = source(filename, samplerate, hop_s)
	samplerate = s.samplerate
	tolerance = 0.8
	pitch_o = pitch("yin", win_s, hop_s, samplerate)
	pitch_o.set_unit("midi")
	pitch_o.set_tolerance(tolerance)
	pitches, confidences = [], []
	# total number of frames read
	total_frames = 0
	while True:
		samples, read = s()
		pitch = pitch_o(samples)[0]
		pitch = round(pitch, 3)
		confidence = pitch_o.get_confidence()
		if confidence < 0.40: pitch = 0
		pitches += [pitch]
		confidences += [confidence]
		total_frames += read
		if read < hop_s: break
	concat = 4
	pitches=pitches[concat:]
	confidences=confidences[concat:]
	return pitches
Example #38
0
    def Extract_Pitch(row, col):

        pitch_List = []
        sample_rate = 44100
        x = np.zeros(44100)
        for i in range(44100):
            x[i] = np.sin(2. * np.pi * i * 225. / sample_rate)

        # create pitch object
        p = aubio.pitch("yin", samplerate=sample_rate)

        # pad end of input vector with zeros
        pad_length = p.hop_size - x.shape[0] % p.hop_size
        x_padded = np.pad(x, (0, pad_length), 'constant', constant_values=0)
        # to reshape it in blocks of hop_size
        x_padded = x_padded.reshape(-1, p.hop_size)

        # input array should be of type aubio.float_type (defaults to float32)
        x_padded = x_padded.astype(aubio.float_type)

        if(row==grid_size.nRow and grid_size==grid_size.nCol):
            for frame, i in zip(x_padded, range(len(x_padded))):
                time_str = "%.2f" % (i * p.hop_size / float(sample_rate))
                pitch_candidate = p(frame)[0] + row + col + 100
                # print(pitch_candidate)
                pitch_List.append(pitch_candidate)
        else:
            for frame, i in zip(x_padded, range(len(x_padded))):
                time_str = "%.2f" % (i * p.hop_size / float(sample_rate))
                pitch_candidate = p(frame)[0] + row + col + 100
                # print(pitch_candidate)
                pitch_List.append(pitch_candidate)
        return pitch_List
Example #39
0
def get_pitches(filename):
    downsample = 1
    # 0 is default sample rate
    samplerate = 0 // downsample

    win_s = 4096 // downsample  # fft size
    hop_s = 512 // downsample  # hop size

    s = source(filename, samplerate, hop_s)
    samplerate = s.samplerate
    tolerance = 0.8
    pitch_o = pitch("yin", win_s, hop_s, samplerate)
    pitch_o.set_unit("Hz")  # Hz
    pitch_o.set_tolerance(tolerance)

    pitches = []
    # total number of frames read
    total_frames = 0
    while True:
        samples, read = s()
        raw_pitch = pitch_o(samples)[0]
        confidence = pitch_o.get_confidence()
        current = Pitch(time=float((total_frames / float(samplerate))), conf=confidence, raw_pitch=raw_pitch)
        hz, octave, note = get_note_octave_deviation(raw_pitch)
        if confidence > 0.8:
            pitches += [current]
            # f.write("hertz: " + str(raw_pitch)+" octave: "+str(octave)+" note: "+ str(note) + "\n")

        total_frames += read
        if read < hop_s:
            return pitches
Example #40
0
 def test_run_on_ones(self):
     " running on ones gives 0 "
     p = pitch('default', 2048, 512, 32000)
     f = fvec(512)
     f[:] = 1
     for _ in range(10):
         assert_equal(p(f), 0.)
Example #41
0
def iter_samples_to_pitches(
        samples, sample_rate=11025, method='yin', buf_size=2048, hop_size=256,
        yield_confidence=False):
    """Yield pitches.

    Parameters
    ----------
    samples : numpy.ndarray
        Samples in Numpy array.

    Yields
    ------
    pitch : float
        Pitch in Hertz
    confidence : float
        Confidence of the pitch detection.

    """
    pitcher = aubio.pitch(method, buf_size, hop_size, int(sample_rate))
    for sample_chunk in iter_chunk(samples):
        pitch = pitcher(sample_chunk.astype(np.float32))[0]
        if yield_confidence:
            confidence = pitcher.get_confidence()
            yield pitch, confidence
        else:
            yield pitch
Example #42
0
def analyzePitch(grain):
    s = source(grain["file"], int(grain["sampleRate"]), int(float(grain["frameCount"])))
    samplerate = s.samplerate
    tolerance = 0.8
    pitch_out = pitch("yin", int(float(grain["frameCount"])), int(float(grain["frameCount"])), samplerate)
    samples, read = s()
    pitchFreq = pitch_out(samples)[0].item() 
    del s 
    return pitchFreq
Example #43
0
 def __init__(self, sample_rate=11025, buf_size=1024, hop_size=256,
              n_mfcc_filters=40, n_mfcc_coeffs=13, pitch_method='yin'):
     """Initialize feature extractor."""
     self._sample_rate = int(sample_rate)
     self._pvoc = aubio.pvoc(buf_size, hop_size)
     self._mfcc = aubio.mfcc(buf_size, n_mfcc_filters, n_mfcc_coeffs,
                             self._sample_rate)
     self._pitch = aubio.pitch(
         method=pitch_method, buf_size=buf_size, hop_size=hop_size,
         samplerate=self._sample_rate)
Example #44
0
File: cmd.py Project: aubio/aubio
 def __init__(self, args):
     self.parse_options(args, self.valid_opts)
     self.pitch = aubio.pitch(**self.options)
     if args.pitch_unit is not None:
         self.pitch.set_unit(args.pitch_unit)
     if args.threshold is not None:
         self.pitch.set_tolerance(args.threshold)
     if args.silence is not None:
         self.pitch.set_silence(args.silence)
     super(process_pitch, self).__init__(args)
    def run_pitch_on_sinusoid(self, method, buf_size, hop_size, samplerate, freq):
        # create pitch object
        p = pitch(method, buf_size, hop_size, samplerate)
        # duration in seconds
        seconds = .3
        # duration in samples
        duration =  seconds * samplerate
        # increase to the next multiple of hop_size
        duration = duration - duration % hop_size + hop_size;
        # build sinusoid
        sinvec = self.build_sinusoid(duration, freq, samplerate)

        self.run_pitch(p, sinvec, freq)
Example #46
0
def getpitches(filename, samplerate):

	#downsample = 1
	#samplerate = 44100 / downsample	
	win_s = 4096 / downsample # fft size
	hop_s = HOP_SIZE  / downsample # hop size

	s = source(filename, samplerate, hop_s)
	samplerate = s.samplerate

	tolerance = 0.8

	pitch_o = pitch("yin", win_s, hop_s, samplerate)
	pitch_o.set_unit("midi")
	pitch_o.set_tolerance(tolerance)

	o = onset("default", win_s, hop_s, samplerate)
	onsets = []

	pitches = []
	confidences = []
	#number = 0
	# total number of frames read
	total_frames = 0
	while True:
	    samples, read = s()
	    pitch1 = pitch_o(samples)[0]
	    #pitch = int(round(pitch))
	    confidence = pitch_o.get_confidence()
	    if o(samples):
        	# print "%f" % o.get_last_s()
        	onsets.append(o.get_last())
	    #if confidence < 0.8: pitch = 0.
	    #print "%f %f %f" % (total_frames / float(samplerate), pitch, confidence)
	    pitches += [pitch1]
	    confidences += [confidence]
	    total_frames += read
	    #number = number + 1
	    if read < hop_s: break

	if 0: sys.exit(0)

	'''
	print onsets
	print pitches
	print confidences
	'''

	
	return pitches, onsets
def getPitch(filename):
    from aubio import pitch
    for f in gb.glob(filename):
            frate, inputdata = sc.read(f)
    downsample = 1
    samplerate = frate / downsample
    window = 1024 / downsample # fft size

    hopsize = 256/ downsample
    sound = source(filename, samplerate, hopsize)
    samplerate = sound.samplerate
    tolerance = 0.8

    # Setting the FFT Algorithm
    pitchlist = pitch("yin", window, hopsize, samplerate)
    pitchlist.set_unit("midi")

    # Setting the tolerance level to 80 percent
    pitchlist.set_tolerance(tolerance)
    total_frames = 0
    pitches = []
    confidences=[]
    while True:
        samples, read = sound()
        pitch = pitchlist(samples)[0]
        confidence = pitchlist.get_confidence()
        confidences+=[confidence]
        pitches += [pitch]
        total_frames += read
        if read < hopsize: break

    # getting the file list of pitch from various sound samples
    pitches = array(pitches[1:])
    confidences = array(confidences[1:])
    cleaned_pitches = pitches

    loudness = abs(an.loudness(inputdata))
    
    # EXtracting all those pitch levels that are above the confidence values
    cleaned_pitches = ma.masked_where(confidences < tolerance, cleaned_pitches,copy=False)
    cleaned_pitches = cleaned_pitches[~cleaned_pitches.mask]

    # condition to check whether there exists a fundamental frequency for the given sound signal
    if len(cleaned_pitches)==0:
        maxValue = 0
    else:
        maxValue = max(cleaned_pitches)
    return maxValue,loudness
Example #48
0
    def __init__(self, options):
        self.algorithm = options.settings['palg']
        self.frame_size = float(options.settings['framesize'])
        self.frame_multiplier = int(options.settings['pframemult'])
        self.hop_multiplier = float(options.settings['phopmult'])
        self.samplerate = float(options.settings['samplerate'])
        self.tolerance = float(options.settings['ptolerance'])
        self.sysexnumber = options.settings['psysexnum']
        self.sysex_command_array = []
        if options.settings['psysexnum'] != 'None':
            for command in options.settings['psysexnum'].split(' '):
                self.sysex_command_array.append(int(command, 0))
        self.control_number = False
        if options.settings['pcontrolnum'] != 'None':
            self.control_number = int(options.settings['pcontrolnum'], 0)
        self.send_note_ons = False
        if options.settings['pnoteon'] == 'True':
            self.send_note_ons = True
        if options.settings['pnoteoff'] == 'True':
            self.send_note_offs = True
        self.count = int(options.settings['pcount'])
        self.low_cutoff = int(options.settings['plowcutoff'])
        self.high_cutoff = int(options.settings['phighcutoff'])
        self.fold_octaves = False
        if options.settings['pfoldoctaves'] == 'True':
            self.fold_octaves = True
        self.num_offset = int(options.settings['pnumoffset'])
        self.midi_processor = None

        self.pitch_object = pitch(self.algorithm,
                                  int(self.frame_size *
                                      self.frame_multiplier),
                                  int(self.frame_size *
                                      self.frame_multiplier *
                                      self.hop_multiplier),
                                  int(self.samplerate))
        if self.tolerance != 'None':
            self.pitch_object.set_tolerance(self.tolerance)
        self.pitch_object.set_unit('midi')
        self.frame_arrays = np.zeros(
            (int(self.frame_multiplier),
             int(self.frame_size)),
            dtype=np.float32)
        self.frame_count = 0
        self.most_pitches = [-1]
        self.pitch_count = 0
        self.last_pitch = 0
Example #49
0
	def pitch_extraction (self, algorithm = "yin", tolerance = 0.95, unit = "midi"):

		self.tolerance = tolerance
		self.unit = unit
		win_s = 1024*4
		hop_s = 512
		pitch_o = aubio.pitch(algorithm, win_s, hop_s, self.samplerate)
		pitch_o.set_unit(self.unit)
		pitch_o.set_tolerance(self.tolerance)

		self._pitches = []
		self._confidences = []

		# total number of frames read
		total_frames = 0

		i = 0
		print "Starting extraction ..."
		while True:
			samples = self._waveform [i*hop_s:(i+1)*hop_s]
			samples = np.float32(samples)
			pitch = pitch_o(samples)[0]
			confidence = pitch_o.get_confidence()
			self._pitches += [pitch]
			self._confidences += [confidence]
			i += 1
			if (i+1)*hop_s > len(self._waveform): break

		self._pitches = np.array(self._pitches).flatten()
		self._filtered_pitches = None
		self._discrete_pitches = None

		plt.plot (self._pitches)
		#for i in range(len(self._beats)):
		#	plt.axvline (self._beats[i], color = 'crimson')
		plt.title ("Pitches")
		plt.show()
		#plt.plot (self._confidences)
		#plt.show()
		self._cleaned_pitches = self._pitches
		self._cleaned_pitches = np.ma.masked_where(self._confidences < self.tolerance, self._cleaned_pitches)
		plt.plot (self._pitches)
		plt.title ("Cleaned Pitches -- tolerance: "+str(self.tolerance))
		plt.show()
Example #50
0
def get_stats_for_pitch_method(method, freqs, samplerate = samplerate):
    """ for a given pitch method and a list of frequency, generate a sinewave
    and get mean deviation """
    means = np.zeros(len(freqs))
    medians = np.zeros(len(freqs))
    for freq, fn in zip(freqs, range(len(freqs))):
        s = sinewave(freq, .50).reshape(-1, hop_size)
        #s = (sinewave(freq, .50) + .0*sinewave(freq/2., .50)).reshape(-1, hop_size)
        p = pitch(method, buf_size, hop_size, samplerate = samplerate)
        candidates = np.zeros(len(s))
        #samples = np.zeros(buf_size)
        for frame, i in zip(s, range(len(s))):
            candidates[i] = p(frame)[0]
        # skip first few candidates
        candidates = candidates[4:]
        means[fn] = np.mean(candidates[candidates != 0] - freq)
        medians[fn] = np.median(candidates[candidates != 0] - freq)
        print (freq, means[fn], medians[fn])
    return means, medians
Example #51
0
def get_pitch(signal, sr, block_size, hop, tolerance = 0.7, unit = 'seconds'):
    pitch_o = aubio.pitch("yin", block_size, hop, sr)
    pitch_o.set_unit('Hz')
    pitch_o.set_tolerance(tolerance)
    signal = signal.astype('float32')
    signal_win = np.array_split(signal, np.arange(hop, len(signal), hop))

    pitches = _find_pitches(signal_win, pitch_o, tolerance)

    if unit == 'seconds':
        pitches = [(frame_idx * hop / sr, value) for frame_idx, value in pitches]
    elif unit == 'samples':
        pitches = [(frame_idx * hop, value) for frame_idx, value in pitches]
    else:
        raise NotImplemented('Unit %s is not implemented', unit)

    if not pitches:
        raise ValueError('No pitches!')

    return pitches
Example #52
0
    def analyzePitches(self, music_file):
        s = source(music_file, self.sample_rate, self.hop_size)
        pitch_o = pitch("yin", self.win_size, self.hop_size, self.sample_rate)
        pitch_o.set_unit("freq")
        pitch_o.set_tolerance(self.tolerance)

        total_frames = 0  # total number of frames read
        while True:
            samples, read = s()
            curr_pitch = pitch_o(samples)[0]
            confidence = pitch_o.get_confidence()
            silence = self.hearingThreshold(curr_pitch)
            self.pitch_data[0].append(curr_pitch)
            self.pitch_data[1].append(confidence)
            self.pitch_data[2].append(silence)

            total_frames += read
            if read < self.hop_size:
                break
        return self.pitch_data
Example #53
0
  def _parse_(self, filename, samplerate):
    s = source(filename, samplerate, self.hop_s)
    
    pitch_o = pitch("default", self.win_s, self.hop_s, samplerate)
    pitch_o.set_unit("midi")

    tempo_o = tempo("default", self.win_s, self.hop_s, samplerate)
    delay = 4. * self.hop_s

    notes = []

    # total number of frames read
    total_frames = 0

    samplerate = float(samplerate)
    previous_samples = []

    while read < hop_s:
      samples, read = s()
      pitch = pitch_o(samples)[0]
      is_beat = tempo_o(samples)

      # BUG: doesn't work if sample starts on beat FIRST
      if is_beat:
        this_beat = int(total_frames - delay + is_beat[0] * hop_s)
        average = sum(previous_samples)/len(previous_samples)
        note, octave = SimpleConverter.MusicalNote.hertz_to_note(average)
        notes += [(note, octave, total_frames/samplerate)]
        previous_samples = []

      # don't want to add otherwise because higher chance at transition zone
      else:
        previous_samples += [pitch]

      total_frames += read

    notes = [SimpleConverter.MusicalNote(note[0] + str(note[1]), note[2]) \
             for note in notes]

    return notes
Example #54
0
    def __init__(self, sample_rate=None, buffersize=None):
        self.sample_rate = sample_rate or self.sample_rate
        self.buffersize = buffersize or self.buffersize
        self.window_size = self.buffersize * 2
        self.stream = None

        self.onset = aubio.onset(
            'specflux', self.window_size, self.buffersize, self.sample_rate)
        self.onset.set_threshold(0.3)
        self.onset.set_silence(-20.)
        self.tempo = aubio.tempo(
            'default', self.window_size, self.buffersize, self.sample_rate)

        self.energy = aubio.specdesc('specflux', self.buffersize * 2)
        self.pv = aubio.pvoc(self.buffersize * 2, self.buffersize)

        self.pitch = aubio.pitch(
            "yinfft", self.window_size, self.buffersize, self.sample_rate)
        self.pitch.set_unit("midi")
        self.pitch.set_tolerance(0.8)

        self.py_audio = pyaudio.PyAudio()
def analyzeHarmonicRatios(grain):
    maxPermissableFreq = 4409
    #Maximum to get 4 harmonics
    numHarmonics = 4
    w = wavefile.load(grain["file"])
    data = w[1][0]
    s = source(grain["file"], w[0], len(data))
    samplerate = s.samplerate

    # Compute the fundamental using the "yin" algorithm
    pitch_o = pitch("yin", len(data), len(data), samplerate)
    samples, read = s()
    fundamental = pitch_o(samples)[0]

    if (fundamental > maxPermissableFreq):
        return None

    # Get the periodogram to get energies at harmonics
    data = data * numpy.hanning(len(data))
    f, Pxx_den = signal.periodogram(data, w[0])
    Pxx_den = 10 * numpy.log10(Pxx_den)
    
    # Set the current harmonic to be twice the fundamental
    fundEnergy = Pxx_den[freqToBin(f, fundamental)]
    curHarm = fundamental * 2
    curHarmCount = 0
    ratios = []

    while(curHarmCount < numHarmonics):
        ratio = fundEnergy / Pxx_den[freqToBin(f, curHarm)]
        #Do not allow infinites, probably caused by 0 energy
        if math.isnan(ratio) or math.isinf(ratio):
            print("Ratio " + str(curHarmCount) + " is " + str(ratio))
            return None
        ratios.append(fundEnergy / Pxx_den[freqToBin(f, curHarm)])
        curHarm += fundamental
        curHarmCount += 1

    return ratios
Example #56
0
def get_pitches(audio, sample_rate=11025, method='yin', buf_size=2048,
                hop_size=256):
    """Return estimated pitches for audio.

    The 'yin' method from the `aubio` module is used for the pitch detection.
    Multiple values are returned, one for each chunk.

    Parameters
    ----------
    audio : numpy.array or pydub.AudioSegment or string
        Audio data.

    Returns
    -------
    pitches : numpy.array
        Array with pitch and confidence information.

    """
    if type(audio) == np.ndarray:
        samples = audio
    elif type(audio) == AudioSegment or type(audio) == str:
        samples = get_samples_mono_11025(audio)
        sample_rate = 11025
    else:
        raise ValueError('audio input has the wrong type')

    pitcher = aubio.pitch(method, buf_size, hop_size, int(sample_rate))

    # Iterate over chunks in the sample
    pitches = []
    for sample_chunk in iter_chunk(samples, chunk_size=hop_size):
        if len(sample_chunk) < hop_size:
            # Zeropadding if too short
            sample_chunk = np.hstack(
                (sample_chunk, np.zeros(hop_size - len(sample_chunk))))
        pitch = pitcher(sample_chunk.astype(np.float32))[0]
        confidence = pitcher.get_confidence()
        pitches.append((pitch, confidence))
    return np.array(pitches)
Example #57
0
def aubiopitch(filename, pitchType, ptolerance, samplerate):
	from aubio import pitch
	win_s = 4096  # fft size
	hop_s = 512   # hop size

	s = source(filename, samplerate, hop_s)
	samplerate = s.samplerate
#	pitch_o = pitch("yin", win_s, hop_s, samplerate)
	pitch_o = pitch(pitchType, win_s, hop_s, samplerate)
	pitch_o.set_unit("freq")
	pitch_o.set_tolerance(ptolerance)

	pitches = []
	confidences = []
	timings = []

	# total number of frames read
	total_frames = 0
	while True:
		samples, read = s()
		pitch = pitch_o(samples)[0]
		#pitch = int(round(pitch))
		confidence = pitch_o.get_confidence()
		#if confidence < 0.8: pitch = 0.
#		print "%f %f %f" % (total_frames / float(samplerate), pitch, confidence)
#		timings += [total_frames / float(samplerate)]
		pitches += [pitch]
		confidences += [confidence]
		total_frames += read
		if read < hop_s: break
	times = [t * hop_s for t in range(len(pitches))]
	for item in times:
		print "%f" % (item/float(samplerate))

	with open('audata_notes','w') as datas:
		for item in times:
			datas.write('\n{0}'.format(item/float(samplerate)))
	return times
p = pyaudio.PyAudio()
instream = p.open(format=FORMAT,
                  channels=CHANNELS,
                  rate=RATE,
                  input=True,
                  frames_per_buffer=hop_s)
outstream = p.open(format=FORMAT,
                   channels=CHANNELS,
                   rate=RATE,
                   output=True,
                   stream_callback = callback)

# set up YIN pitch tracking algo
tolerance = 0.8
pitch_o = aubio.pitch("yin", win_s, hop_s, RATE)
pitch_o.set_unit("freq")
pitch_o.set_tolerance(tolerance)

print "Recording"

try:
    outstream.start_stream()
    print "Finished?: " + str(outstream.is_active())
    while True:
        data_str = instream.read(hop_s)
        data = numpy.fromstring(data_str,'Float32')
        pitch = pitch_o(data)[0]
        print "Pitch: %.5f Hz"%pitch
        for i in xrange(len(data_str)):
            frameBuffer.append(data_str[i])
filename = sys.argv[1]

downsample = 1
samplerate = 44100 / downsample
if len( sys.argv ) > 2: samplerate = int(sys.argv[2])

win_s = 4096 / downsample # fft size
hop_s = 512  / downsample # hop size

s = source(filename, samplerate, hop_s)
samplerate = s.samplerate

tolerance = 0.7

pitch_o = pitch("yin", win_s, hop_s, samplerate)
pitch_o.set_unit("midi")
pitch_o.set_tolerance(tolerance)

pitches = []
confidences = []

# total number of frames read
total_frames = 0
while True:
    samples, read = s()
    pitch = pitch_o(samples)[0]
    #pitch = int(round(pitch))
    confidence = pitch_o.get_confidence()
    #if confidence < 0.8: pitch = 0.
    #print "%f %f %f" % (total_frames / float(samplerate), pitch, confidence)