kaplay icon indicating copy to clipboard operation
kaplay copied to clipboard

feat: Channels for audio playing

Open amyspark-ng opened this issue 1 year ago • 8 comments

Is your feature request related to a problem? Please describe.

Let's say you'd like to have channels like sfx and music to play them on different volumes, that'd be kinda hard since you'd have to write them from zero (which i've done)

Describe the solution you'd like

Something along the lines of

play({ channel: "sfx" })

This way you can manage different sound volumes

amyspark-ng avatar Sep 24 '24 16:09 amyspark-ng

This is an interesting idea.

It might be good to implement, but I bet you could already do something like this pretty quickly by connecting it to a common gain node, now that #383 is done

const K = kaplay({global: false})

const sfxChannel = new GainNode(K.audioContext)
sfxChannel.connect(K.audioContext.destination);
const musicChannel = new GainNode(K.audioContext)
musicChannel.connect(K.audioContext.destination);
// and so on

// to use a channel
K.play("foo", { connectTo: sfxChannel });

only problem is that you aren't able to connect the gain nodes to KAPLAY's master node that way... waiting on #400 for that

dragoncoder047 avatar Sep 26 '24 19:09 dragoncoder047

Interesting, i managed to do something with creating custom classes and volumes from gamesaves and etc, but seems like a good approach

amyspark-ng avatar Sep 26 '24 19:09 amyspark-ng

This is an interesting idea.

It might be good to implement, but I bet you could already do something like this pretty quickly by connecting it to a common gain node, now that #383 is done

const K = kaplay({global: false})

const sfxChannel = new GainNode(K.audioContext)
sfxChannel.connect(K.audioContext.destination);
const musicChannel = new GainNode(K.audioContext)
musicChannel.connect(K.audioContext.destination);
// and so on

// to use a channel
K.play("foo", { connectTo: sfxChannel });

only problem is that you aren't able to connect the gain nodes to KAPLAY's master node that way... waiting on #400 for that

Can't we do that internally while looking for a good way of give people globals? Something simpler like Amy said, but that internally uses your method

Would be a great PR

lajbel avatar Sep 27 '24 12:09 lajbel

class AudioChannel {
    muted: boolean,
    volume: number,
}

function playAudio(opts: audioPlayOpt & { channel: AudioChannel }) {
    play({
        volume: opts.channel.muted ? 0 : opts.channel.volume
    })
}

is what im doing right now, i store this channels in a game save so you can set volumes, is this a good API? im not sure

amyspark-ng avatar Sep 27 '24 14:09 amyspark-ng

    play({
        volume: opts.channel.muted ? 0 : opts.channel.volume
    })

is what im doing right now, i store this channels in a game save so you can set volumes, is this a good API? im not sure

It'll work for one-off and short sounds, but if you have longer sounds that might have the volume of the channel changed or muted while the sound is playing, it will not update, since the volume attribute of the audioPlay returned is not pointing to the same as the AudioChannel. You could try to jerry rig something with a Proxy but that would probably be too much work.

dragoncoder047 avatar Sep 27 '24 15:09 dragoncoder047

const audioPlayObject =play({
   volume: opts.channel.muted ? 0 : opts.channel.volume
   })
return audioPlayerObject

wouldn't this work? you could get the audioPlay object and then modify the pitch

amyspark-ng avatar Sep 27 '24 15:09 amyspark-ng

return audioPlayerObject

wouldn't this work? you could get the audioPlay object and then modify the pitch

Well, you'd kind of have to do it manually --

const myChannel: AudioChannel = ...;
const player: AudioPlay = playAudio("boom", { channel: myChannel });

// this doesn't update player.volume
myChannel.volume = ...;

// to change the sound mid-play
player.volume = ...;

dragoncoder047 avatar Sep 27 '24 15:09 dragoncoder047

well i'd use channels to manage sound effects volume and music volume sepparetely, idk about you, but changing it sepparetely doesn't look so bad

amyspark-ng avatar Sep 27 '24 15:09 amyspark-ng