Git Product home page Git Product logo

Comments (3)

li-car-fei avatar li-car-fei commented on September 16, 2024

I try to write a function for it, but I do not know is it right :

def write_midi(words, path_outfile, word2event):
    class_keys = word2event.keys()
    midi_obj = miditoolkit.midi.parser.MidiFile()

    # global tempo
    midi_obj.tempo_changes.append(TempoChange(tempo=119, time=0))

    bar_cnt = 0

    all_notes = []

    for i in range(len(words)):
        vals = []
        for kidx, key in enumerate(class_keys):
            vals.append(word2event[key][words[i][kidx]])
        # print(vals)

        if vals[0].split(' ')[-1] == 'New':
            bar_cnt += 1
        if (vals[0].split(' ')[-1] == 'New') or (vals[0].split(' ')[-1] == 'Continue'):
            position_ = vals[1].split(' ')[-1]
            if position_ != "<PAD>" and position_ != "<MASK>":
                position = int(position_.split('/')[0]) - 1
            else:
                continue

            duration_ = vals[3].split(' ')[-1]
            if duration_ != "<PAD>" and duration_ != "<MASK>":
                duration = int(duration_) * 60
                if duration == 0:
                    duration = 60
            else:
                continue

            pitch_ = vals[2].split(' ')[-1]
            if pitch_ != "<PAD>" and pitch_ != "<MASK>":
                pitch = int(pitch_)
            else:
                continue

            st = bar_cnt * BAR_RESOL + position * TICK_RESOL

            et = st + duration
            all_notes.append(miditoolkit.Note(velocity=60 pitch=pitch, start=st, end=et))

    # save midi
    piano_track = Instrument(0, is_drum=False, name='piano')
    piano_track.notes = all_notes
    midi_obj.instruments = [piano_track]
    midi_obj.dump(path_outfile)


the velocity is set to default 60

from midi-bert.

sophia1488 avatar sophia1488 commented on September 16, 2024

Hi sorry for the late reply. Are you using version 1 (midi-score) or version 2 (midi-performance)?
For version 1, please refer to this code. It extracts melody MIDI given original MIDI, and you can adjust it to suit your needs.
If it's version 2, I have not pushed the code yet, and I'll try to update it this week.

I didn't run your code above, but it looks fine. If you can preprocess your generated midi to the same CP, then it should be alright!

from midi-bert.

li-car-fei avatar li-car-fei commented on September 16, 2024

Is the duration value multiplied by 60 and then output? In this case, the maximum duration will reach 3600, is it too long?

from midi-bert.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.