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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add render() method to get raw sample data. #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

chr15m
Copy link

@chr15m chr15m commented May 17, 2021

Hi @feross, thanks for this wonderful library which I'm using over at https://dopeloop.ai/melody-generator. For a different application I wanted to be able to get the raw audio samples of the rendered MIDI file, and this patch adds that functionality with the render() method. It returns a pair of Float32Arrays containing the left and right channel audio data, which can then be used to mix with other audio, compress to mp3, or whatever. I also updated the documentation. I know you are super busy, so thank you for considering this PR for merging. 馃檹

Here is some test code for trying out the render() method:

const Timidity = require('timidity')

const player = new Timidity()

player.load('/pachelbel_canon.mid')

player.on("progress", function(value, total) {
  console.log("progress", value, "/", total)
});

player.on("loaded", function() {
  console.log("duration:", player.duration)
  console.log("song:", player._currentUrlOrBuf)
  const arrays = player.render()
  console.log(arrays)

  var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
  var myArrayBuffer = audioCtx.createBuffer(2, arrays[0].length, audioCtx.sampleRate);
  for (var channel = 0; channel < myArrayBuffer.numberOfChannels; channel++) {
    myArrayBuffer.copyToChannel(arrays[channel], channel)
  }
  var source = audioCtx.createBufferSource();
  source.buffer = myArrayBuffer;
  source.connect(audioCtx.destination);
  source.start();
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant