def test_conversions(self): """ of frequencies to midi note numbers and ansi note names. """ from pygame.midi import (frequency_to_midi, midi_to_frequency, midi_to_ansi_note) self.assertEqual(frequency_to_midi(27.5), 21) self.assertEqual(frequency_to_midi(36.7), 26) self.assertEqual(frequency_to_midi(4186.0), 108) self.assertEqual(midi_to_frequency(21), 27.5) self.assertEqual(midi_to_frequency(26), 36.7) self.assertEqual(midi_to_frequency(108), 4186.0) self.assertEqual(midi_to_ansi_note(21), 'A0') self.assertEqual(midi_to_ansi_note(102), 'F#7') self.assertEqual(midi_to_ansi_note(108), 'C8')
def __init__(self, midi_output): self.data = midi_output[0] self.timestamp = midi_output[1] self._event_type = self.data[0] #key press or release self.note_number = self.data[1] self.velocity = self.data[2] self.frequency = midi.midi_to_frequency(self.note_number)
def test_conversions(self): """of frequencies to midi note numbers and ansi note names.""" from pygame.midi import frequency_to_midi, midi_to_frequency, midi_to_ansi_note self.assertEqual(frequency_to_midi(27.5), 21) self.assertEqual(frequency_to_midi(36.7), 26) self.assertEqual(frequency_to_midi(4186.0), 108) self.assertEqual(midi_to_frequency(21), 27.5) self.assertEqual(midi_to_frequency(26), 36.7) self.assertEqual(midi_to_frequency(108), 4186.0) self.assertEqual(midi_to_ansi_note(21), "A0") self.assertEqual(midi_to_ansi_note(71), "B4") self.assertEqual(midi_to_ansi_note(82), "A#5") self.assertEqual(midi_to_ansi_note(83), "B5") self.assertEqual(midi_to_ansi_note(93), "A6") self.assertEqual(midi_to_ansi_note(94), "A#6") self.assertEqual(midi_to_ansi_note(95), "B6") self.assertEqual(midi_to_ansi_note(96), "C7") self.assertEqual(midi_to_ansi_note(102), "F#7") self.assertEqual(midi_to_ansi_note(108), "C8")
def generate_wave_list(self): waves = [] # list of waves for e in self.midi_events: if e.status == 144: # press event start_time = e.timestamp end_time = next( x for x in self.midi_events if x.status == 128 and x.data1 == e.data1).timestamp duration = (end_time - start_time) / 1000 # from ms to s frequency = midi.midi_to_frequency(e.data1) velocity = e.data2 / 127 # vel ranges from 1 to 127 waves.append(Wave(frequency, dur=duration, vol=velocity)) return waves
def update(self): """ To be called in a pygame event loop. """ audios = [] while not self.audio_queue.empty(): # print('-----audio from queue') # print(time.time() - time_start) audio = self.audio_queue.get() audios.append(audio) if audios: pg.event.post(pg.event.Event(AUDIOS, audios=audios)) onsets = [] while not self.oneset_queue.empty(): onsets.append(self.oneset_queue.get()) if onsets: pg.event.post(pg.event.Event(ONSETS, onsets=onsets)) pitches = [] notes = [] frequencies = [] while not self.pitch_queue.empty(): # print("pitch, confidence", pitch, confidence, midi_to_ansi_note(pitch)) pitch = self.pitch_queue.get() pitches.append(pitch) notes.append(midi_to_ansi_note(pitch)) frequencies.append(midi_to_frequency(pitch)) if pitches: pg.event.post( pg.event.Event(PITCHES, pitches=pitches, notes=notes, frequencies=frequencies))
def generate_wave(self): return Wave(midi.midi_to_frequency(self.data1), vol=self.data2 / 127)
format=pyaudio.paInt16, output=True, frames_per_buffer=BUFFER_SIZE) # -- RUN THE SYNTH -- try: print("Starting...") notes_dict = {} while True: if notes_dict: # Play the notes samples = get_samples(notes_dict) samples = np.int16(samples).tobytes() stream.write(samples) if midi_input.poll(): # Add or remove notes from notes_dict for event in midi_input.read(num_events=16): (status, note, vel, _), _ = event if status == 0x80 and note in notes_dict: del notes_dict[note] elif status == 0x90 and note not in notes_dict: freq = midi.midi_to_frequency(note) notes_dict[note] = get_sin_oscillator(freq=freq, amp=vel / 127) except KeyboardInterrupt as err: midi_input.close() stream.close() print("Stopping...")
import pyaudio import numpy as np from math import pi, sin from pygame import midi from itertools import count midi.init() mi=midi.Input(device_id=midi.get_default_input_id()) st=pyaudio.PyAudio().open(44100,1,pyaudio.paInt16,output=True,frames_per_buffer=256) try: nd={} while True: if nd:st.write(np.int16([sum([int(next(osc)*32767) for _,osc in nd.items()]) for _ in range(256)]).tobytes()) if mi.poll(): for(s,n,v,_),_ in mi.read(16): if s==0x80 and n in nd:del nd[n] elif s==0x90 and n not in nd:nd[n]=(sin(c)*v*0.1/127 for c in count(0,(2*pi*midi.midi_to_frequency(n))/44100)) except KeyboardInterrupt as err: mi.close() st.close()