Playing Sounds

Posted by Sickent on Fri 16 Apr 2021 11:32 AM — 5 posts, 17,559 views.

USA #0
I think I'm doing something dumb, but please mercy. I'm trying to get a sound to play through a script and I'm getting nil in return.

I have test.wav in ~/sounds/.

If I make a script.lua, and only put

Sound ("test.wav")

Into it, when I load the script, nothing happens and nothing is played.

Trying

require "check"
Note (check (Sound ("test.wav")))

Just gives me a blank line, no eOk or error.

I'm not sure if it matters, but I can make an alias to play the sound and MUSHClient plays the sound just fine. Assuming the .wav was created correctly, I'm super confused why I'm not at least getting some kind of confirmation out of check or sound in general.

Thank you.
Australia Forum Administrator #1
Try doing:


print (GetInfo (74))


That should tell you your default sounds directory.

Also try:


print (check (PlaySound (1, "test.wav")))


I found that Sound did not give an error, whereas PlaySound did, if the file wasn't there.




Test your script (that is, the PlaySound line) by using the Immediate window (Ctrl+I) so there isn't any doubt that there is not some other problem with the scripting.
Australia Forum Administrator #2
After searching through the source I found an interesting thing. Sound uses the Windows API call PlaySound to play .wav files, and the documentation for PlaySound says:

Quote:

If it cannot find the specified sound, PlaySound uses the default system event sound entry instead. If the function can find neither the system default entry nor the default sound, it makes no sound and returns FALSE.


Thus, if the file isn't found it plays the default beep (which I was hearing) and returns an OK return, so you get no error message.

However, the MUSHclient script function PlaySound (confusingly, the same name as the API function) plays sounds a different way, and thus returns "file not found" if it can't find the file. This different way allows for multiple sounds at once, volume changes, panning etc.

Thus I think your problem is that the file isn't found, most likely because the sound file isn't in the default directory.
Amended on Fri 16 Apr 2021 10:20 PM by Nick Gammon
Australia Forum Administrator #3
Sickent said:

Trying

require "check"
Note (check (Sound ("test.wav")))

Just gives me a blank line, no eOk or error.


The design for "check" is that it doesn't return an error message for eOk, otherwise you would be getting a confusing "No error" message every time you did a check. You may have seen such stuff with Windows at times: "You have got an error: No error".

If you omitted "check" from that line it would have printed 0.
USA #4
Nick Gammon said:
If it cannot find the specified sound, PlaySound uses the default system event sound entry instead. If the function can find neither the system default entry nor the default sound, it makes no sound and returns FALSE.

This explains a lot, and I did something dumb with the file structure. You're right. Thank you for the help.