ID:164872
 
I haven't messed with sound() too much since the implementation of fmod for BYONDs sound handling needs. I understand that we now have a sound datum. I've starting using it, but am not fully aware of what all it does.

All I'm really using it for now is saving whatever BGM the player is listening to so that when they log out and back in the same BGM is played.

Create the var using sound datum
mob/var/sound/music


Play the sound through the new music variable, In this example it's in my character creation system right before it moves them to the starter town.
        NewChar.loc = locate(/turf/start)
M.client.mob = NewChar
NewChar.music = sound('Mus07.mid', repeat=1, channel=1, wait=0)
NewChar << NewChar.music


And play it again when loading the char (code not really required there).

Question is, how do I actually use it? Can I actually manipulate the sound that's currently playing with it?

I read the reference entry for vars (sound), and it led me to believe that I could do something like this:

mob/verb/lowervolume()
usr.music.volume=50


It didn't actually do anything though, any suggestions?

what exactly can I do with music now? Is it possible for me to add verbs that do stuff like music.volume=(whatever) to adjust the volume while it's playing or maybe something that does music.repeat=0 if the player is sick and tired of hearing the same bgm over and over again? How about stop and play?
I can't believe you people! It's been almost TWENTY FIVE minutes and no one has helped me!

Just kidding. :/ I got it resolved.

Turns out you gotta resend it. I would love if you didn't have to, since it restarts the current song, but ohh well.
mob/verb/lowervolume(N as num)
set desc = "(\"number to decrement volume\") Lowers the volume of the currently playing BGM."
music.volume-=N
usr << music


Got a nifty little statpanel for music now too that shows all the music properties, Gonna make it so you can click each one to modify it directly. :)
In response to Zagreus
Looking at the reference, the volume var is a percentage from 0 to 100. But what if N is a negative number, less than -100? Then, you have an invalid volume. On top of that, they'll have to make sure that their volume doesn't go over, which not everyone will want to do, so they'll enter large numbers. Uou can address this problems in a simple way: With the use of max + min. You can also make a single verb that can raise AND lower the volume without much effort.

To do this, you can create a simple macro, called limit.

#define limit(num, lowerbound, upperbound) max(min(num, upperbound), lowerbound)


This macro can be used all over to simplify many tasks, and can overall improve your code.
In response to Audeuro
Thanks, I had another solution in mind (Not actually keeping that verb I showed earlier either, that was just a test).

I like your solution better than what I was thinking of though. Thanks for sharing dude.
mob
var/sound/music
New()
.=..()
music=new(volume=100)
music.status|=SOUND_UPDATE //will only take effect if the player actually received the music first
verb
volume(n as num|null)
music.volume=max(0,min(n,100))
if(music.file) src<<music //if they're already listening to music: updates the volume on the client-side\
level and makes them hear at a different volume. if they weren't, send the sound and act as if SOUND_UPDATE wasn't activated
In response to Zagreus
Zagreus wrote:

> mob/verb/lowervolume(N as num)
> set desc = "(\"number to decrement volume\") Lowers the volume of the currently playing BGM."
> music.volume-=N
> usr << music
>

Try:


 mob/verb/lowervolume(N as num)
set desc = "(\"number to decrement volume\") Lowers the volume of the currently playing BGM."
music.volume-=N
music.status= SOUND_UPDATE
usr << music
In response to Texter
That works great. Thanks for the help!

Edit:

In case anyone else tries this, just some advice. Remove the SOUND_UPDATE flag after you resend the music, or else it stays on and can cause problems such as your music not changing anymore.

mob/verb/setvolume(N as num)
set desc = "(\"number to set volume to (0-100)\") Adjusts the volume of the BGM."
music.volume=limit(N,0,100)
music.status |= SOUND_UPDATE
usr << music
music.status &= ~SOUND_UPDATE