chiptune2.js icon indicating copy to clipboard operation
chiptune2.js copied to clipboard

Volume support

Open RubenSandwich opened this issue 8 years ago • 7 comments

It would be helpful to be able to change the volume of the music being played.

RubenSandwich avatar Mar 02 '16 17:03 RubenSandwich

Actually one should be able to wire the internal ScriptProcessorNode to any WebAudio sink. So you could use a GainNode to control volume. I'll think about ways of doing so.

Currently the internal ScriptProcessor Node is directly wired to the AudioDestination (speakers) with no nice way to intercept between. However you can try the hackish approach:

// create a GainNode
var gain = player.context.createGain()

// wire gain to speaker output
gain.connect(player.context.destination)

// disconnect internal ScriptProcessorNode from speakers
player.currentPlayingNode.disconnect()

// wire script processor to gain
player.currentPlayingNode.connect(gain)

// make it quieter
gain.gain.value = 0.5

You will have to do the rewiring after each call to play(), because it creates a new currentPlayingNode. I'll think of a nicer way of doing that.

deskjet avatar Mar 02 '16 23:03 deskjet

Cool that works great. Thanks. Maybe make it part of the API though so it would be more easily accessible and hide the implementation details.

RubenSandwich avatar Mar 04 '16 16:03 RubenSandwich

Note that you can also set the gain directly in libopenmpt via openmpt::module::set_render_param.

sagamusix avatar Mar 13 '16 21:03 sagamusix

@deskjet I once merged several input source to one output just by copying and adding the buffer to the output processor. Maybe you can use this method to maintain one currentPlayingNode. Another solution to this is using MediaSourceExtension, which also solves the requirement for seek bar.

ClassicOldSong avatar Jul 11 '17 08:07 ClassicOldSong

Note that you can also set the gain directly in libopenmpt via openmpt::module::set_render_param.

How exactly? I'm struggling to change the volume currently using any means

KingPossum avatar Jul 28 '21 09:07 KingPossum

@KingPossum Here is how set_render_param is called to configure libopenmpt.

https://github.com/deskjet/chiptune2.js/blob/05349a0b4a83e3e5192ef287eb769fd23210a1e9/chiptune2.js#L146-L147

Try RENDER_MASTERGAIN_MILLIBEL as the first parameter. If that is not defined use the value 1 as that's what it should be according to the original source code. The second paramter would be your desired gain in milliBels (e.g. -600 to lower the volume)

https://github.com/OpenMPT/openmpt/blob/324e20cb74eacd0c758a251ac1ffdc264aeb24b3/libopenmpt/libopenmpt.h#L809-L815

deskjet avatar Jul 28 '21 10:07 deskjet

@KingPossum Here is how set_render_param is called to configure libopenmpt.

https://github.com/deskjet/chiptune2.js/blob/05349a0b4a83e3e5192ef287eb769fd23210a1e9/chiptune2.js#L146-L147

Try RENDER_MASTERGAIN_MILLIBEL as the first parameter. If that is not defined use the value 1 as that's what it should be according to the original source code. The second paramter would be your desired gain in milliBels (e.g. -600 to lower the volume)

https://github.com/OpenMPT/openmpt/blob/324e20cb74eacd0c758a251ac1ffdc264aeb24b3/libopenmpt/libopenmpt.h#L809-L815

Thank you so much for the quick and concise reply! I hope you can forgive my ignorance on the topic, I'm new to JS and website creation in general. Here's the current syntax I'm attempting to use, and it's still having no effect. I'm sure I'm just not using it correctly.

libopenmpt._openmpt_module_set_render_param(1, -600);

KingPossum avatar Jul 28 '21 11:07 KingPossum