Esempio n. 1
0
    def tune(self):
        pDetection = aubio.pitch("default", 2048, 1024, 44100)
        pDetection.set_unit('Hz')
        pDetection.set_silence(-40)

        p = pyaudio.PyAudio()

        stream = p.open(format=pyaudio.paFloat32,
                        channels=1,
                        rate=44100,
                        input=True,
                        frames_per_buffer=1024)

        while self.is_running == True:

            data = stream.read(1024)

            samples = np.frombuffer(data, dtype=aubio.float_type)
            pitch = pDetection(samples)[0]

            if pitch != 0:
                pitch_cent = round(
                    1200 * log(pitch / note2freq(freq2note(pitch)), 2), 2)
                self.note_text = freq2note(pitch)
                self.cent_text = str(pitch_cent)
                if pitch_cent < 0:
                    self.posy = .45
                else:
                    self.posy = .65
                self.color_red = round((abs(pitch_cent) * .051), 3)
                self.color_green = round(((abs(pitch_cent) * -.051) + 2.55), 3)
                print(f"Green: {self.color_green} Red: {self.color_red}")
            #print(freq2note(pitch))

        raise Exception("Thread Terminated")  # Terminates the thread
Esempio n. 2
0
def get_note():
    new = True
    count = 0
    for pitch in get_pitches(get_file()):
        try:
            pitchName = aubio.freq2note(pitch)
        except ValueError:
            pass
        else:
            if new:
                pitchold = pitch
                pitcholdName = aubio.freq2note(pitch)
                new = False
            elif pitchName == pitcholdName:
                count += 1
            else:
                new = True
                yield music.Note(pitchold, count)
                count = 0
    #need to add error handling(abuio does not like low inputs from noise)
    try:
        pitchStart = pitchold = pitch = get_pitches(get_file())
    except ValueError:
        noteName = 'error'
    if pitch == 0:
        noteName = 'rest'
    else:
        noteName = aubio.freq2note(pitch)
    count = 0
    while aubio.freq2note(pitch) == aubio.freq2note(pitchold):
        count += 1
        pitchold = pitch
    return Note(pitchStart, count)
Esempio n. 3
0
File: tuner.py Progetto: mdx97/tuner
def profile_audio(audio_data):
    """
    Returns the frequency, closest note, and the frequency of the closest note for the given audio data.
    """
    samples = np.frombuffer(audio_data, dtype=aubio.float_type)
    frequency = 2 * pitch_detect(samples)[0]
    closest_note = aubio.freq2note(frequency)
    closest_note_frequency = aubio.note2freq(closest_note)
    return (frequency, closest_note, closest_note_frequency)
Esempio n. 4
0
def detectNote():
    # modified version of record function taken from 112 website
    CHUNK = 1024 #measured in bytes looks like its the buffer size
    FORMAT = pyaudio.paFloat32
    CHANNELS = 1 #changed from og stereo
    RATE = 44100 #common sampling frequency
    RECORD_SECONDS = 0.1 # in seconds, human reaction time is .17s for audio

    p = pyaudio.PyAudio()

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

    frames = []

    for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
        data = stream.read(CHUNK)
        frames.append(data)

    # setup pitch
    tolerance = 0.8
    hop_s = CHUNK # hop size
    fftSize = 4096 # fft size
    pitchBase = aubio.pitch("default", fftSize, hop_s, RATE)
    pitchBase.set_unit("freq")
    pitchBase.set_tolerance(tolerance)

    audioBuffer = stream.read(CHUNK)
    signal = np.fromstring(audioBuffer, dtype=np.float32)
    pitch = pitchBase(signal)[0]
    confidence = pitchBase.get_confidence()

    # ensures that only audible notes are counted as pitch
    if 10 <= pitch <= 10000:
        
        note = aubio.freq2note(pitch)
    else:
        note = ''
        
    stream.stop_stream()
    stream.close()
    p.terminate()

    return note
def audio_callback(indata, frames, time, status):
    # TODO THIS IS MAIN BUTTON LOGIC

    values = np.array([value[0] for value in indata[0:1136]])
    my_pitch = pitch_o(values)[0]
    note = aubio.freq2note(my_pitch)
    if note == 'F2':
        print('A')
        ahk.key_down('s')
        ahk.key_up('s')
    elif note == 'G2':
        print('B')
        ahk.key_down('a')
        ahk.key_up('a')
    elif note == 'A#2':
        print('Left')
        ahk.key_down('Left')
        ahk.key_up('Left')
    elif note == 'C3':
        print('Right')
        ahk.key_down('Right')
        ahk.key_up('Right')
    elif note == 'E3':
        print('Down')
        ahk.key_down('Down')
        ahk.key_up('Down')
    else:
        print('OFF')
    #print(note)

    #for i in range(0, len(indata), 1):
        #print(indata[i][0])
        #print('break')
        #print(pitch_o(indata[i:512][0]))
    """This is called (from a separate thread) for each audio block."""
    if status:
        print(status, file=sys.stderr)
    # Fancy indexing with mapping creates a (necessary!) copy:
    q.put(indata[::args.downsample, mapping])
Esempio n. 6
0
 def test_freq2note_under(self):
     " make sure freq2note(439) == A4 "
     self.assertEqual("A4", freq2note(439))
Esempio n. 7
0
 def test_freq2note_above(self):
     " make sure freq2note(441) == A4 "
     self.assertEqual("A4", freq2note(441))
Esempio n. 8
0
###use same process dynamically for live input
##def get_lenth(pitches):
##    count = 0
##    first = True
##    pitchold = None
##    for pitch in pitches:
##        pitch = aubio.freq2note(pitch)
##        if pitch == pitchold:
##            count += 1
##        else:
##            pitchold = pitch
##            if not first:
##                yield (pitchold, count)
##            first = False
##            count = 0
        
#fname = get_file(5)
#time.sleep(1)
for pitch in get_pitches('noise_music.wav'):
    print pitch
    try: print aubio.freq2note(pitch)
    except: print 'error'
#os.remove(fname)

'''
reults:
silence: 0.0 pitch, c-1
quiet noise: between 0 and 50. error
mid noise: 
'''
Esempio n. 9
0
 def test_freq2note(self):
     " make sure freq2note(441) == A4 "
     self.assertEqual("A4", freq2note(441))
Esempio n. 10
0
 def test_freq2note_under(self):
     " make sure freq2note(439) == A4 "
     self.assertEqual("A4", freq2note(439))
Esempio n. 11
0
def make_frequency_to_note_neural_net():
    # all variables are prefixed with f2n so that the tensorflow session can
    # differentiate between the variables relevant to this neural net and the
    # chord neural net.

    # setting up inputs and outputs
    f2n_inputs = tf.placeholder(tf.float32, [None, 1])
    f2n_outputs = tf.placeholder(tf.float32, [None, 12])

    # setting up weights and biases for inputs->hiddens
    f2n_weights_in = tf.Variable(tf.random_normal([1, 15], stddev=0.03),
                                 name='f2n_weights_in')
    f2n_biases_in = tf.Variable(tf.random_normal([15]), name='f2n_biases_in')

    # setting up weights and biases for hiddens->outputs
    f2n_weights_out = tf.Variable(tf.random_normal([15, 12], stddev=0.03),
                                  name='f2n_weights_out')
    f2n_biases_out = tf.Variable(tf.random_normal([12]), name='f2n_biases_out')

    # setting up math for hidden layer output
    f2n_hidden_out = tf.add(tf.matmul(f2n_inputs, f2n_weights_in),
                            f2n_biases_in)
    f2n_hidden_out = tf.nn.relu(f2n_hidden_out)

    # setting up math for outputs
    f2n_output_values = tf.nn.softmax(
        tf.add(tf.matmul(f2n_hidden_out, f2n_weights_out), f2n_biases_out))

    # setting up math for backpropagation
    f2n_outputs_clipped = tf.clip_by_value(f2n_output_values, 1e-10, 0.9999999)
    #f2n_cross_entropy = -tf.reduce_mean(tf.reduce_sum(f2n_outputs * tf.log(f2n_outputs_clipped) + (1 - f2n_outputs) * tf.log(1 - f2n_outputs_clipped), axis=1))
    f2n_cross_entropy = -tf.reduce_sum(
        f2n_outputs * tf.log(f2n_outputs_clipped))

    #set up optimizer
    f2n_optimizer = tf.train.GradientDescentOptimizer(0.003)
    f2n_train_step = f2n_optimizer.minimize(f2n_cross_entropy)

    # set up initialisation operator
    f2n_init = tf.global_variables_initializer()

    # function for correct prediction
    f2n_correct_prediction = tf.equal(tf.argmax(f2n_outputs, 1),
                                      tf.argmax(f2n_output_values, 1))

    # function for accuracy
    f2n_accuracy = tf.reduce_mean(tf.cast(f2n_correct_prediction, tf.float32))

    #create session
    f2n_sess = tf.Session()

    #initialize session variables
    f2n_sess.run(f2n_init)

    #set up saver to save network
    saver = tf.train.Saver()

    batch_size = 100

    #test each octave individually
    for octave in range(0, 6):

        #print("octave: " + str(octave))

        #calculate the start and enc of each octave
        start = 110 * pow(2, octave)
        end = 110 * pow(2, octave + 1)

        for epoch in range(0, 10000):

            #establish list for the batches of inputs and outputs
            f2n_freq = [0] * batch_size
            correct_out = [None] * batch_size

            #create batches
            for i in range(batch_size):
                correct_out[i] = [0] * 12
                train_freq = random.randrange(start, end)
                f2n_freq[i] = [normValue(start, end, train_freq)]

                #find desired note, continue if frequency is invalid
                try:
                    f2n_desired = aubio.freq2note(train_freq)[:-1]
                except:
                    continue

                try:
                    correct_out[i][noteDict[f2n_desired] - 1] = 1
                except:
                    continue

            #find accuracy and loss using session
            a, c, _ = f2n_sess.run(
                [f2n_accuracy, f2n_cross_entropy, f2n_train_step],
                feed_dict={
                    f2n_inputs: f2n_freq,
                    f2n_outputs: correct_out
                })

            #print data every 1000th iteration
            if (epoch + 1) % 1000 == 0:
                #create test data
                f2n_freq_test = [0] * batch_size
                correct_out_test = [None] * batch_size
                for i in range(batch_size):
                    correct_out_test[i] = [0] * 12
                    test_freq = random.randrange(start, end)
                    f2n_freq_test[i] = [normValue(start, end, test_freq)]

                    #find desired note, continue if frequency is invalid
                    try:
                        f2n_desired = aubio.freq2note(test_freq)[:-1]
                    except:
                        continue

                    try:
                        correct_out_test[i][noteDict[f2n_desired] - 1] = 1
                    except:
                        continue
                #a_test, _ = f2n_sess.run([f2n_accuracy, f2n_cross_entropy], feed_dict={f2n_inputs: f2n_freq_test, f2n_outputs: correct_out_test})

                #print("Train Data Loss: " + str(c))
                #print("Train Data Accuracy: " + str(100.00*a) + "%")
                #print("Test Data Accuracy: " + str(100.00*a_test) + "%")
                #print("Test input data: ")
                #for entry in f2n_freq_test:
                #    print(entry)
                #print()
    save_path = saver.save(f2n_sess, "checkpoints/f2nBP.ckpt")

    return f2n_sess
Esempio n. 12
0
def testF2N():
    global F2C
    if F2C == False:
        times = 1
    else:
        times = 3
    # all variables are prefixed with f2n so that the tensorflow session can
    # differentiate between the variables relevant to this neural net and the
    # chord neural net.

    # setting up inputs and outputs
    f2n_inputs = tf.placeholder(tf.float32, [None, 1])
    f2n_outputs = tf.placeholder(tf.float32, [None, 12])

    # setting up weights and biases for inputs->hiddens
    f2n_weights_in = tf.Variable(tf.random_normal([1, 15], stddev=0.03),
                                 name='f2n_weights_in')
    f2n_biases_in = tf.Variable(tf.random_normal([15]), name='f2n_biases_in')

    # setting up weights and biases for hiddens->outputs
    f2n_weights_out = tf.Variable(tf.random_normal([15, 12], stddev=0.03),
                                  name='f2n_weights_out')
    f2n_biases_out = tf.Variable(tf.random_normal([12]), name='f2n_biases_out')

    # setting up math for hidden layer output
    f2n_hidden_out = tf.add(tf.matmul(f2n_inputs, f2n_weights_in),
                            f2n_biases_in)
    f2n_hidden_out = tf.nn.relu(f2n_hidden_out)

    # setting up math for outputs
    f2n_output_values = tf.nn.softmax(
        tf.add(tf.matmul(f2n_hidden_out, f2n_weights_out), f2n_biases_out))

    # setting up math for backpropagation
    f2n_outputs_clipped = tf.clip_by_value(f2n_output_values, 1e-10, 0.9999999)
    #f2n_cross_entropy = -tf.reduce_mean(tf.reduce_sum(f2n_outputs * tf.log(f2n_outputs_clipped) + (1 - f2n_outputs) * tf.log(1 - f2n_outputs_clipped), axis=1))
    f2n_cross_entropy = -tf.reduce_sum(
        f2n_outputs * tf.log(f2n_outputs_clipped))

    #set up optimizer
    f2n_optimizer = tf.train.GradientDescentOptimizer(0.003)
    f2n_train_step = f2n_optimizer.minimize(f2n_cross_entropy)

    # set up initialisation operator
    f2n_init = tf.global_variables_initializer()

    # function for correct prediction
    f2n_correct_prediction = tf.equal(tf.argmax(f2n_outputs, 1),
                                      tf.argmax(f2n_output_values, 1))

    # function for accuracy
    f2n_accuracy = tf.reduce_mean(tf.cast(f2n_correct_prediction, tf.float32))

    #create session
    f2n_sess = f2nBP.make_frequency_to_note_neural_net()

    #initialize session variables
    f2n_sess.run(f2n_init)

    #set up saver to save network
    #saver = tf.train.Saver()
    #saver.restore(f2n_sess, "checkpoints/f2nBP.ckpt")

    output = [''] * times
    for j in range(times):

        prompt = "enter integer value between %d and %d " % (MIN_VAL, MAX_VAL)
        intInput = getNetworkVal(prompt)
        #notYetImpl()
        # give note predicted

        for i in range(0, 7):
            if 110 * pow(2, i) <= intInput <= 110 * pow(2, i + 1):
                start = 110 * pow(2, i)
                end = 110 * pow(2, i + 1)

        correct_out = [0] * 12
        f2n_freq = [f2nBP.normValue(start, end, intInput)]
        f2n_desired = aubio.freq2note(intInput)[:-1]
        args = f2n_sess.run([f2n_output_values],
                            feed_dict={f2n_inputs: [f2n_freq]})
        maxEntry = 0
        maxVal = 0
        out = args[0][0]
        for i in range(len(out)):
            if out[i] > maxVal:
                maxVal = out[i]
                maxEntry = i

        for entry in noteDict.keys():
            if noteDict[entry] == maxEntry + 1:
                output[j] = entry
                print("Predicted note for frequency %d is %s" %
                      (intInput, entry))
    return output
Esempio n. 13
0
print("Starting to listen, press Ctrl+C to stop")

# main loop
while True:
    try:
        # read data from audio input
        _, data = recorder.read()
        # convert data to aubio float samples
        samples = np.fromstring(data, dtype=aubio.float_type)
        #plt.plot(samples)
        #plt.show()
        if len(samples):
            # pitch of current frame
            freq = pitcher(samples)[0]
            try:
                note = aubio.freq2note(freq)
                midi = aubio.note2midi(note)
                volume = np.sum(samples**2) / len(samples)
                vel = (volume - VOL_OFFSET) / (1 - VOL_OFFSET) * 112
                if volume > VOL_OFFSET:
                    print("{:3} {}".format(note, volume))
                    if last_midi != midi:
                        midiout.send_message([0x90, midi, vel])  # note on
                        print("midi: " + str(midi))
                    last_midi = midi
                elif volume < .6:
                    midiout.send_message([CONTROL_CHANGE, ALL_SOUND_OFF, 0])
                    last_midi = 0
            except ValueError:
                print("warn: ValueError")
    except KeyboardInterrupt: