def create_beat( self, samples: Dict[float, NDArray[Float32]] = None, sample_rate: int = 44100, ) -> AudioFile: """ Create a beat from the pattern in the sequencer. """ if samples is not None: raise ValueError( "Samples are not needed to create a beat for an MidiSequencer!" ) mid = PrettyMIDI(initial_tempo=self.bpm) drum_track = Instrument(program=0, is_drum=True, name="drums") mid.instruments.append(drum_track) for row in self.pattern: for note in row: if note is not None: drum_track.notes.append(note) return AudioFile( np.array(mid.fluidsynth(fs=sample_rate), dtype=np.float32), self.bpm, sample_rate, )
def pm_to_wav(pm: PrettyMIDI, filename: str, rate=16000): waveform = pm.fluidsynth(fs=rate) wavfile.write(filename, rate, waveform)
def _make_pcm(self, pm: pretty_midi.PrettyMIDI): audio_data = pm.fluidsynth() return f64le_to_s32le(audio_data)
transposition = 12 - keys.index(key) # transpose leftward transposed_generated_music = transpose_sequence(generated_music, \ transposition = transposition) transposed_seed_music = transpose_sequence(seed_music, transposition = \ transposition) bpm = st.sidebar.number_input('Set the bpm (beats per minute) in the '\ 'range [20, 180]', min_value = 20, max_value = 180,\ value = 60, key = 'bpm') # st.sidebar.write('bpm = {}'.format(bpm)) for music_type in ['seed', 'generated']: if (st.button('Create MIDI File for the ' + music_type.title() + \ ' Music', key = music_type)): fpath = './midi_output/{0}_{1}.mid'.format(music_type, session_id) exec('convert_to_midi(transposed_{0}_music, '.format(music_type) + \ 'bpm = bpm, output_file = fpath)') midi_data = PrettyMIDI(fpath) audio_data = midi_data.fluidsynth() audio_data = np.int16( audio_data / np.max(np.abs(audio_data)) * 32767 * 0.9 ) # -- Normalize for 16 bit audio https://github.com/jkanner/streamlit-audio/blob/main/helper.py virtualfile = BytesIO() wavfile.write(virtualfile, 44100, audio_data) st.write('Play temporary .wav file:') st.audio(virtualfile) st.markdown(get_binary_file_downloader_html(fpath, 'MIDI'), unsafe_allow_html=True)