0

I'm creating a midi file based on frequencies. I'm using ToneJS (in Node.js) to create the midi file with this code:

exports.createMidiFile = (req,res,next) => {
  // create a new midi file
var midi = new Midi()
// add a track
const track = midi.addTrack()
track.addNote({
  midi : Tone.Frequency(400).toMidi(),
  time : 0,
  duration: 10
})

// write the output
fs.writeFileSync("output.mid", new Buffer(midi.toArray()))
}

What im trying to do is a for loop to add notes to the same track. I tried:

for (var i = 0; i < length; i++){
track.addNote({
  midi : Tone.Frequency(i).toMidi(),
  time : 0,
  duration: 10
})
}

But when i try to open the file it says the file is damaged..

Thanks in advance friends.

Elna Haim
  • 545
  • 1
  • 5
  • 19

1 Answers1

0

Try writing your loop like this :

for (var i = 0; i < length; i++){
  track = track.addNote({
    midi : Tone.Frequency(i).toMidi(),
    time : 0,
    duration: 10
  })
}

Doing this makes it equivalent to chaining the calls to addNote()

Mouradif
  • 2,666
  • 1
  • 20
  • 37