deskjet / chiptune2.js

much like chiptune.js - but newer and neater
382 stars 50 forks source link

Volume support #17

Open RubenSandwich opened 8 years ago

RubenSandwich commented 8 years ago

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

deskjet commented 8 years ago

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.

RubenSandwich commented 8 years ago

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.

sagamusix commented 8 years ago

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

ClassicOldSong commented 7 years ago

@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.

KingPossum commented 3 years ago

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

deskjet commented 3 years ago

@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

KingPossum commented 3 years ago

@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);