Jump to content

Adjust volume while headless


yaksplat

Recommended Posts

yaksplat

Found a handy way to change the volume of the pi, without needing to open up a window.

amixer -q -M sset Headphone {volume}%

This will adjust the volume for the device Headphone, but use PCM for the HDMI port.  85% is the max, before the audio becomes distorted.

        public async Task<string> SetVolume(string ip, int volume)
        {
            string retval;
            retval = "";

            using (var client = new SshClient(ip, 22, "pi", "raspberry"))
            {
                client.Connect();
                try
                {
                    client.RunCommand($"amixer -q -M sset Headphone {volume}%");
                }
                catch (SshConnectionException)
                {

                    retval = "Disconnected";
                }
            }
            return retval;
        }

 

  • Like 1
Link to comment
Share on other sites

yaksplat

Better yet, i found that there's an extension class on Renci.SshNet to make async calls

 

The fade is nice and gradual, not an instant change.

using Renci.SshNet.Async;

  public async Task<string> SetVolume(string ip, int volume)
        {
            string retval;
            retval = "";

            using (var client = new SshClient(ip, 22, "pi", "raspberry"))
            {
                client.Connect();
                try
                {
                    string command = $"amixer -q -M sset Headphone {volume}%";
                    var cmd = client.CreateCommand(command);
                    var response = await cmd.ExecuteAsync();
                }
                catch (SshConnectionException)
                {

                    retval = "Disconnected";
                }
            }
            return retval;
        }


        public async Task<string> FadeVolume(string ip, int volume)
        {
            int StartingVolume = await GetVolume(ip);

            if(StartingVolume > volume)
            {
                for (int i = StartingVolume; i >= volume; i-=1)
                {
                    await SetVolume(ip, i);
                }
            }
            else
            {
                for (int i = StartingVolume; i <= volume; i += 1)
                {
                    await SetVolume(ip, i);
                }
            }

            return null;
        }


        public async Task<int> GetVolume(string ip)
        {
            int vol=0;

            using (var client = new SshClient(ip, 22, "pi", "raspberry"))
            {
                client.Connect();
                try
                {
                    string command = "amixer -M sget Headphone |grep % |awk '{print $4}'|sed 's/[^0-9]//g'";
                    var cmd = client.CreateCommand(command);
                    var response = await cmd.ExecuteAsync();
                    if (response != null)
                    {
                        Int32.TryParse(response.TrimEnd("/n".ToCharArray()),out vol);
                    }
                }
                catch (SshConnectionException)
                {

                }
            }
            return vol;
        }

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...