Play a sound with Rhino

Hello,

We are working on an experimental project with Rhino, and we have an idea for which we would like to have your opinion.

The idea would be to have a sound played according to an action in Rhino.
Example : When you type “ExtrudeCrv” and press Enter, a file sample.mp3 is being played.

For now, we’ve explored Python libraries (like playsound · PyPI for example).
But what about the feasability of such a script with RhinoPython (which, as we are aware, is not exactly python but ironpython) ?

Thank you,

Hi @antoine3,

Here are a couple of simple examples:

import System
System.Media.SystemSounds.Asterisk.Play()
import System
wav = r'C:\Windows\Media\notify.wav'
player = System.Media.SoundPlayer(wav)
player.Play()

– Dale

1 Like

Hello @dale ,
Thank you for your answer!
It looks like it would work in Windows only, am I correct?
Is there a solution that would work on macOS? (And even better, one that would work for both Windows and macOS?)

No idea - give it a try.

– Dale

I tried both of them on mac.
It doesn’t play any sound, but interestingly it doesn’t show any error.
I can’t try it on a windows right now but I’ll also try it on windows.
Thank you for your answer, that’s a good start anyway, and shows that it could be doable :slight_smile:

Could you please share a method that works for macOS? Because now I’m looking for a solution too.

Hi,
This works on macOS:

import subprocess

audio = 'path_to_your_file.wav'
subprocess.Popen(["afplay", audio])

afplay is specific to macOS though, so this won’t work on Windows.
Hope this helps,

1 Like
   protected virtual void OnClick(EventArgs e)
        {
           
            if (!PlatformCheck.IsWindows())
            {
                try
                {
                    string audio = "/Users/Shared/Grasshopper Gold/food4rhino/Startup.wav";
                    Process.Start("afplay", audio);

                }
                catch (Exception ex)
                {
                    MessageBox.Show($"An error afplay: {ex.Message}", "Error", MessageBoxButtons.OK);
                }
            }

            Click?.Invoke(this, e);
        }

It doesn’t work for me. I don’t even have an error to figure out why. :roll_eyes: Macos 14.1. cpu m1

I’m not familiar with C#, so I’m afraid I can’t do much more to help :confused:
The only thing I would double check is your path that contains a space? :man_shrugging: Maybe the space character should be escaped?
In Python, when the script can’t access the audio file it also does not throw any error, so that would be the only hint I can give.

1 Like

string audio = "\"/Users/Shared/Grasshopper Gold/food4rhino/Startup.wav\"";
Yeah, thanks. It’s working now.