kevincennis / Mix.js

Mutitrack mixing in JavaScript with the Web Audio API
http://kevvv.in/mix/
MIT License
323 stars 76 forks source link

Control Track's fader function ? #20

Closed jeromebg closed 7 years ago

jeromebg commented 7 years ago

Hi, just love your mix app, looks gorgeous ! I would like to be able to control the track's fader via javascript, something like : fade(track1,10); Is it already implemented ? Would like to use it in a Studio virtual Tour : https://www.360images.fr/360/blackbox

Many thanx !

kevincennis commented 7 years ago

Something like this will work. It's hacky, but probably the best you can do without having to jump in and actually modify the source of Mix.js itself.

// get a Track model by index
const track = App.mix.get('tracks').at( 0 );

// pass a Track model instance, gain value (0-1), and duration of the fade in seconds
function fade( track, target, duration ) {
  const curr  = App.ac.currentTime;
  const time  = curr + duration;
  const gain  = track.nodes.gain.gain;
  const view  = App.trackViews.children.findByModel( track );
  const fader = view.$el.find('.fader');

  gain.linearRampToValueAtTime( target, time );

  const end = Date.now() + ( duration * 1000 );

  function render() {
    if ( Date.now() <= end ) {
      const top = App.util.scale( Math.sqrt( gain.value ), 0, 1.15, 220, 0 );
      track.set( 'gain', gain.value, { silent: true } );
      fader.css({ top: `${ top }px` });
      requestAnimationFrame( render );
    }
  }

  render();
}

// fade the track to a gain value of 1 over the course of 3 seconds
fade( track, 1, 3 );
jeromebg commented 7 years ago

Thanx a lot for you fast reply Kevin ! Unfortunatly I'm a newbee in javacsript, I tried your code but I have an error : ReferenceError: can't access lexical declaration `track' before initialization I guess I messed up somewhere ? https://www.360images.fr/360/blackbox/mix/test.html

jeromebg commented 7 years ago

And also it can be very long before the loading bar starts to move, does the script has to preload all the audio files before starting playing them ?

kevincennis commented 7 years ago

Yes. Web Audio cannot decode partial MP3 files, so playback cannot begin until all files have been loaded

jeromebg commented 7 years ago

Ok, got it, thx for your reply ! What about the reference error ? A clue ?

spmurrayzzz commented 7 years ago

@jeromebg the Issues section in this repository is not for requests to debug the code you write, nor is it for feature requests. We can help give you direction on certain things, but debugging/troubleshooting is outside of the scope of work we are willing to commit in our spare time.