Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exported midi tempo #40

Closed
correa-coder opened this issue Oct 9, 2022 · 4 comments
Closed

Exported midi tempo #40

correa-coder opened this issue Oct 9, 2022 · 4 comments
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@correa-coder
Copy link

In the web demo there's a slider that allows you to choose the midi file tempo, however, today I installed the CLI tool with pip and I liked it because it's a lot faster, but I couldn't find a way to change the midi tempo, it would be nice if it had a parameter like basic-pitch <output> <input> --midi-tempo 75 for instance. Currently, it always exports the file at 120 bpm so I need to manually stretch the notes in the DAW to fit the desired tempo.

@rabitt rabitt added enhancement New feature or request good first issue Good for newcomers labels Oct 27, 2022
@rabitt
Copy link
Contributor

rabitt commented Oct 27, 2022

Great point @correa-coder ! This is now on our backlog - but in the meantime if anyone wants to try this we're happy to take a PR for this.

@correa-coder
Copy link
Author

Hello, I made a workaround using Mido to change the MIDI tempo without affecting the note duration/speed.

import math
import os
import mido

MIDI_DIR = "C:\\Users\\UserName\\Desktop"
MIDI_INPUT_NAME = "piano_basic_pitch.mid"
MIDI_OUTPUT_NAME = f"{MIDI_INPUT_NAME.replace('.mid', '')}_with_tempo.mid"
TARGET_BPM = 60  # new BPM to use in the exported file

mid = mido.MidiFile(os.path.join(MIDI_DIR, MIDI_INPUT_NAME))

# get original file tempo, defauts to 120 BPM (500000 microseconds per beat)
midi_tempo = mid.tracks[0][0].tempo

# change midi tempo to new BPM
mid.tracks[0][0].tempo = mido.bpm2tempo(TARGET_BPM)

for i, track in enumerate(mid.tracks):
    if i == 0:
        # skipping first track because it only contains time signature and tempo information
        continue
    print(f"Track {i} - {track.name}")
    for msg in track:
        if msg.type == 'note_on' or msg.type == 'note_off':
            # convert note lengths to fit new BPM without changing the speed of the song
            old_msg_time = msg.time
            new_msg_time = old_msg_time * (midi_tempo / mido.bpm2tempo(TARGET_BPM))
            msg.time = math.floor(new_msg_time)

mid.save(os.path.join(MIDI_DIR, MIDI_OUTPUT_NAME))

To test I created a .wav file using FL Studio. It plays this chord progression: C G/B Gm/Bb Dm7 G7 C at 60 BPM

piano_roll_1

After running basic-pitch it creates a file at 120 BPM which is double of the tempo of the recording, so quarter notes became half notes, eight notes became quarter notes and so on...

piano_roll_2

After running the script, it changes the BPM back to 60 and the notes got closer to the original durations

piano_roll_3

@johngao2
Copy link
Contributor

johngao2 commented Feb 7, 2023

Hi! Thanks for raising this, we have a PR with the new CLI parameter completed and awaiting testing. I'll let you know once it gets patched in and you can give it a try

@johngao2
Copy link
Contributor

Hello! Thanks for your patience. The midi tempo CLI argument has been added in version 0.2.3 (the latest version). Please feel free to update your package and give it a try. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants