0

I'm trying to change a value on runtime via a range input but I do not get it to work. I get no errors. When I change the frequence in onSlide the sound does still play on 800. It should change.

import $ from 'jquery';
import rangeslider from 'rangeslider.js';
import T from 'timbre';

window.$ = $;

    $(document).ready(() => {

        var f = 800;

        T("sin", {freq:f, mul:0.5}).play(); /*plays a continous sound*/

        $('input[type="range"]').rangeslider({
            polyfill : false,
            onSlide: function() {
                var ff = 440; /*changing frequancy, but the tone is still the same.*/
                f = ff;
            }
        });

    });
TGrif
  • 5,725
  • 9
  • 31
  • 52
user2952238
  • 749
  • 2
  • 11
  • 36
  • 1
    Changing the variable `f` does not call `T` again. What is `T`, can you alter an existing instance? – Bergi Dec 22 '17 at 10:06
  • Sorry, edited the code. T is an instance of timbre.js. a sound library. – user2952238 Dec 22 '17 at 10:08
  • then play() will be called on every change event the slider gets. Wich will just get stuck in a loop of plays()'s – user2952238 Dec 22 '17 at 10:10
  • `f` is passed by value not reference. So changing `f` after it's been used isn't going to change anything (the memory now points to a different location). You need a method in `T` that will change the frequency (I don't know the library) adjusting the variable is going to do nothing. Concentrate on finding a method in timbre to adjust the frequency as this is a dead end. – Liam Dec 22 '17 at 10:11
  • Documentation: https://mohayonao.github.io/timbre.js/ I cant find anyting relative to what i want to do. – user2952238 Dec 22 '17 at 10:12
  • Then you can't do it. (that said I have never used this library so I don't know for sure) – Liam Dec 22 '17 at 10:12

1 Answers1

1

As shown in Getting Started, you need to store the instance in a variable so that you can call the set method later.

$(document).ready(() => {
    const tone = T("sin", {freq:f, mul:0.5}).play(); /*plays a continous sound*/
    $('input[type="range"]').rangeslider({
        polyfill : false,
        onSlide: function() {
            tone.set( {freq: 440 }); /* changing frequency of the tone */
        }
    });
});
Bergi
  • 630,263
  • 148
  • 957
  • 1,375