def new_note_to_play(): self.midi_notes[k] = note_off_time self.scored_notes[k] = self.music_time new_note_on = Message("note_on") new_note_on.note = k new_note_on.velocity = 100 self.devices.output_messages.append(new_note_on)
def build_note_off(channel,note,velocity,time): m = Message("note_off") m.channel = channel m.note = note m.velocity = velocity m.time = time return m
def appendNote(self, note, velocity, time, seconds = False): msg = Message("note_on") msg.note = note msg.velocity = velocity ## time (s) * (ticks / beat) * (beats / min) * (1min / 60s) msg.time = time if seconds == False else int(round(time / self.seconds)) self.appendMsg(msg)
def create_key_note(note_val: int, note_on: bool): if self.menu.is_menu_active(Menus.GAME): note_name = "note_on" if note_on == False: note_name = "note_off" new_note = Message(note_name) new_note.note = note_val new_note.velocity = 100 self.devices.input_messages.append(new_note)
def shiftAndScale(inputFileName, outputFileName, pitchShift, timeScale): orig = MidiFile(inputFileName) mid = MidiFile() for tr in orig.tracks: track = MidiTrack() mid.tracks.append(track) #<meta message track_name name='Grand Piano' time=0> # time=0 could be replaced be time=timeShift if necessary msg = Message('note_on', note=0, velocity=0, time=0) track.append(msg) for msg in tr: if msg.type == 'note_on': msg.time = round(msg.time * timeScale) msg.note = msg.note + pitchShift track.append(msg) mid.save(outputFileName)
def encoding_to_message(encoding, decodings, defaults=master_defaults): message = decodings[encoding] if message == "START_TRACK": #header defaults time_signature = MetaMessage("time_signature") time_signature.numerator = defaults["time_signature.numerator"] time_signature.denominator = defaults["time_signature.denominator"] time_signature.clocks_per_click = defaults[ "time_signature.clocks_per_click"] time_signature.notated_32nd_notes_per_beat = defaults[ "time_signature.notated_32nd_notes_per_beat"] key_signature = MetaMessage("key_signature") key_signature.key = defaults["key_signature.key"] set_tempo = MetaMessage("set_tempo") set_tempo.tempo = defaults["set_tempo.tempo"] return [time_signature, key_signature, set_tempo] if message == "END_TRACK": return [MetaMessage('end_of_track')] if message == "WAIT_A_BEAT": return [message] if message.startswith("note"): parts = message.split("-") res = Message(parts[0]) res.note = int(parts[1]) res.time = int(parts[2]) # defaults res.channel = defaults["note.channel"] res.velocity = defaults["note.velocity"] return [res] return []
def update(self, dt): self.menu.update(dt, self.music_running) if self.menu.running == False: self.quit() def score_vfx(note_id = None): self.score_fade = 1.0 self.menu.set_event("score_vfx") if note_id is not None: spawn_pos = [-0.71, self.staff.note_positions[note_id]] self.particles.spawn(2.0, spawn_pos, [0.37, 0.82, 0.4, 1.0]) # Handle events from MIDI input, echo to output so player can hear for message in self.devices.input_messages: if message.type == "note_on" or message.type == "note_off": self.devices.output_messages.append(message) # Process any output messages and transfer them to player notes down for message in self.devices.output_messages: if message.type == "note_on": self.player_notes_down[message.note] = 1.0 if self.mode == MusicMode.PAUSE_AND_LEARN: if message.note in self.scored_notes: score_vfx(message.note) time_diff = self.music_time - self.scored_notes[message.note] self.score += max(10 - time_diff, 0) del self.scored_notes[message.note] elif message.type == "note_off": del self.player_notes_down[message.note] # Light up score box for any held note for note, velocity in self.player_notes_down.items(): if velocity > 0.0: self.staff.set_score(note) self.devices.input_messages = [] tempo_recip_60 = 1.0 / 60.0 game_draw, _ = self.menu.is_menu_active(Menus.GAME) if game_draw: music_notes = self.music.draw(dt, self.music_time, self.note_width_32nd) music_time_advance = dt * Song.SDQNotesPerBeat * (tempo_recip_60 * self.music.tempo_bpm) if self.music_running: self.music_time += music_time_advance # Play the backing track in sync with the player self.music.update(self.dt, self.music_time, self.devices) # Process all notes that have hit the play head music_notes_off = {} for k in music_notes: note_off_time = 0 note_entry = music_notes[k] if isinstance(note_entry, list): note_off_time = note_entry[0] else: note_off_time = note_entry # Highlight the note box to show this note should be currently played if note_off_time >= self.music_time: self.staff.note_on(k) def new_note_to_play(): self.midi_notes[k] = note_off_time self.scored_notes[k] = self.music_time new_note_on = Message("note_on") new_note_on.note = k new_note_on.velocity = 100 self.devices.output_messages.append(new_note_on) # The note value in the dictionary is the time to turn off if k in self.midi_notes: if self.music_time >= note_off_time: music_notes_off[k] = True else: new_note_to_play() # Send note off messages for all the notes in the music for k in music_notes_off: del music_notes[k] self.staff.note_off(k) new_note_off = Message("note_off") new_note_off.note = k self.devices.output_messages.append(new_note_off) self.midi_notes.pop(k) if self.mode == MusicMode.PERFORMANCE: if self.staff.is_scoring(): if self.score_fade < 0.5: for note in self.scored_notes: score_vfx(note) break self.score += 10 ** self.dt elif self.mode == MusicMode.PAUSE_AND_LEARN: if len(self.scored_notes) > 0 and self.music_running: self.music_time -= music_time_advance self.staff.draw(dt) # Show the play mode mode_string = "Performance" if self.mode == MusicMode.PERFORMANCE else "Pause & Learn" self.font_game.draw(f"{mode_string}", 16, [0.5, 0.8], [0.6, 0.6, 0.6, 1.0]) # Show the score on top of everything self.score_fade -= dt * 0.5 self.font_game.draw(f"{math.floor(self.score)} XP", 22, [self.bg_score.sprite.pos[0] - 0.025, self.bg_score.sprite.pos[1] - 0.03], [0.1, 0.1, 0.1, 1.0]) # Update and flush out the buffers self.devices.update() self.devices.output_messages = []