Script for playing sound through lms.

Moderator: leecollings

Post Reply
User avatar
capman
Posts: 153
Joined: Friday 12 July 2013 20:48
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Belgium
Contact:

Script for playing sound through lms.

Post by capman »

I found a script you can use for playing some mp3 file through your logitech media server (lms) on my NAS.
Thanks to Felix Mueller ,SlimServer announcement script http://www.gwendesign.com/slimserver/dev_software.htm.
Just copy the script to your scripts folder and name it (mine is deurbel1.pl).
Fill in your ip of your lms and cli port (standard 9090). Tell where your mp3 file is to be playing (mine is /volume1/Music/Deurbel/Christmas.mp3).
You can set if you want the mixervolume on line 173.
Now make in domoticz a dummy switch and paste in the 'on action' the name of the script (script:///volume1/@appstore/domoticz/var/scripts/deurbel1.pl).
So when the switch is on , you here your sound through the speakers. A use it for the doorbell ringing.
Note: when music playing (radio, etc...) the music paused , mp3 sound come through and then the music unpaused and plays on.

Hope someone can use it.
Happy 2017 !!

Code: Select all

#!/usr/bin/perl

# ********************************************************************
# Simple announcement script for SlimServer using the CLI         v0.6
#
# Copyright (c) 2003 Felix Mueller (felix.mueller(at)gwendesign.com)
#
# This file might be distributed under the same terms as the
# SlimServer (www.slimdevices.com)
#
# Description:
# Plays a sound and shows a message interupting the current song
# - Store current parameters
# - The sound is appended to the current playlist and played
# - After the duration of the sound the sound is removed
# - Resume playing the current song
# - Restore current parameters
#
# Plattform tested:
# - Linux
#
# Known restrictions:
# - trancoded files (i.e. ogg) will restart at the beginning after
#    the announcement
# - a shuffled playlist will be reshuffled
# - does not work with iTunes (maybe works only with real SLIMP3)
#
# History:
# 0.6 - Added parameters for user/password (thanks to Mario Wagner)
# 0.5 - Adapted and tested with SlimServer 5.1.1
# 0.4 - Added command line parameter: --name --server --ip --line1 --line2 --sound
#     - Added player selection by name
# 0.3 - Added power and volume persistency
# ********************************************************************

use strict;

use Getopt::Long;
use IO::Socket;


# Parameter defaults overridden by command line
my $szServerIP		= "your ip here";
my $iServerCLIPort	= 9090;
my $szPlayerName	= "Keuken";
my $szLine1		= "Line1 text";
my $szLine2		= "Line2 text";
my $szSound		= "/volume1/Music/Deurbel/Christmas.mp3";	# path on the machine where announcement.pl runs
my $user		= "";
my $passw		= "";   


# Internal parameters
my $debug		= 0;	# 0 = off, 1 = on

GetOptions( 	"server:s"	=>	\$szServerIP,
		"port:i"	=>	\$iServerCLIPort,
		"name:s"	=>	\$szPlayerName,
		"line1:s"	=>	\$szLine1,
		"line2:s"	=>	\$szLine2,
		"user:s"	=>	\$user,
		"passw:s"	=>	\$passw,
		"sound:s"	=>	\$szSound);

# Check for a player name
showUsage() if( $szPlayerName eq "");

# Prepare variables
$szPlayerName = encodeChars( $szPlayerName);
$szLine1 = encodeChars( $szLine1);
$szLine2 = encodeChars( $szLine2);
$szSound = encodeChars( $szSound);

# Try to open a connection to the SLIMP3 CLI
my $socket = IO::Socket::INET->new(	PeerAddr => $szServerIP,
					PeerPort => $iServerCLIPort,
					Proto => "tcp",
					Type => SOCK_STREAM);
# Check if socket was opened successful
if( !defined( $socket))
{
	print "Cannot connect to $szServerIP:$iServerCLIPort\n";
	exit;
}

# Login
if ($user ne "")
{
	my $login = sendAndReceive("login $user $passw");
	print $login;
	$debug && print "user: $user, passw: $passw\n";
}

# Get the # of players
my $iNumPlayer = sendAndReceive( "player count ?");
$debug && print "iNumPlayer: $iNumPlayer\n";

# Get the player ip and port from the name
my $szPlayerIPAndPort = "";
for( my $i = 0; $i < $iNumPlayer; $i++)
{
	if( $szPlayerName eq sendAndReceive( "player name $i ?"))
	{
		$szPlayerIPAndPort = sendAndReceive( "player address $i ?");
		last;
	}
}
$debug && print "szPlayerName: $szPlayerName\n";

# Check if successful
if( $szPlayerIPAndPort eq "")
{
	print "Player with name: $szPlayerName could not be found!\n";
	close( $socket);
	exit;
}
$debug && print "szPlayerIPAndPort: $szPlayerIPAndPort\n";

# Get power status (on, off)
my $iPower = sendAndReceive( "power ?");
$debug && print "iPower: $iPower\n";

# Get shuffle status (on, off)
my $iShuffle = sendAndReceive( "playlist shuffle ?");
$debug && print "iShuffle: $iShuffle\n";

# Turn shuffle off
sendAndReceive( "playlist shuffle 0");

# Get mode (play, stop, pause)
my $iMode = sendAndReceive( "mode ?");
$debug && print "iMode: $iMode\n";

# Get current song
my $iIndex = sendAndReceive( "playlist index ?");
$debug && print "iIndex: $iIndex\n";

# Get path and filename to current song
my $iPath = sendAndReceive( "playlist path $iIndex ?");
$debug && print "iPath: $iPath\n";

# Check the song file type (mp3, ogg, wav)
my $iMP3 = 0;
$iMP3 = 1 if( $iPath =~ m/mp3$/);
$debug && print "iMP3: $iMP3\n";

# Get the current position (in s)
my $iTime = sendAndReceive( "time ?");
$debug && print "iTime: $iTime\n";

# Stop the current song
sendAndReceive( "stop");

# Add the announcement sound file at the end of the playlist
sendAndReceive( "playlist append $szSound");


$debug && print "szSound: $szSound\n";

# Get the number of tracks in the playlist
my $iTracks = sendAndReceive( "playlist tracks ?");
$iTracks -= 1;
$debug && print "iTracks: $iTracks\n";

# Is now -1 if playlist was empty
$iTracks = 0 if( $iTracks < 0);

# Get the volume
my $iVolume = sendAndReceive( "mixer volume ?");
$debug && print "iVolume: $iVolume\n";

# Set a volume for the announcement
sendAndReceive( "mixer volume 65");

# Play the announcement sound
sendAndReceive( "playlist index $iTracks");

# Get the duration of the announcement sound
my $iDuration = sendAndReceive( "playlist duration $iTracks ?");
$debug && print "iDuration: $iDuration\n";

sleep( 1);

# Show the announcement message
sendAndReceive( "display $szLine1 $szLine2 $iDuration");

# Wait until announcement sound has finished
sleep( $iDuration);

# Stop it
sendAndReceive( "stop");

# Mute output
sendAndReceive( "mixer volume 0");

# Remove the announcement sound from the playlist
sendAndReceive( "playlist delete $iTracks");

# Reselect the old song
sendAndReceive( "playlist index $iIndex");

# Set the player in the old mode
if( $iMode eq "stop")
{
	sendAndReceive( "stop");
}
elsif( $iMode eq "pause")
{
	if( $iMP3 == 1)
	{
		sendAndReceive( "time $iTime");
		sleep( 1);
	}
	sendAndReceive( "mode pause");
}
elsif( $iMode eq "play")
{
	if( $iMP3 == 1)
	{
		sendAndReceive( "time $iTime");
	}
}

# Reinstate the volume
sendAndReceive( "mixer volume $iVolume");

# Reinstate the shuffle mode
sendAndReceive( "playlist shuffle $iShuffle");

# Reinstate the power mode
sendAndReceive( "power $iPower");

close( $socket);

# ---------------------------------------------
sub sendAndReceive
{
	my $cmd = shift;

	return if( $cmd eq "");

#	$cmd = $szPlayerIP . encodeChars( ":") . $iPlayerPort . " " . $cmd;

	if( $szPlayerIPAndPort ne "")
	{
		$cmd = $szPlayerIPAndPort . " " . $cmd;
	}
	print $socket "$cmd\n";
	my $answer = <$socket>;
	$answer =~ s/$cmd //i;
	$answer =~ s/\n//;

	return $answer;
}

# ---------------------------------------------
sub encodeChars
{
	my $myChars = $_[0];

	my $MetaChars = quotemeta(' ;,/?\|=+)(*&^%$#@!~`:');
	
	$myChars =~ s/([$MetaChars\"\'\x80-\xFF])/"%" . uc( sprintf( "%2.2x", ord($1)))/eg;

	return $myChars;
}

# ---------------------------------------------
sub showUsage
{
	print "usage: slimp3announcement.pl <parameters>\n";
	print "\n";
	print "Mandatory parameter:\n";
	print "--name=<player name>\ti.e.: \"my slimp3\"\n";
	print "\n";
	print "Optional parameters:\n";
	print "--server=<ip>\t\tdefault: 127.0.0.1\n";
	print "--port=<nr>\t\tdefault: 9090\n";
	print "--line1=<text>\t\tdefault: \"Line1 text\"\n";
	print "--line2=<text>\t\tdefault: \"Line2 text\"\n";
	print "--user=<text>\t\tdefault: \"\"\n";
        print "--passw=<text>\t\tdefault: \"\"\n";
 	print "--sound=<soundfile>\tdefault: /music/_Diverses/gotmail19.mp3\n";
	exit;
}

Last edited by capman on Monday 02 January 2017 10:05, edited 1 time in total.
Skippiemanz
Posts: 230
Joined: Sunday 14 July 2013 20:21
Target OS: Linux
Domoticz version: 4.10233
Location: Alkmaar, The Netherlands
Contact:

Re: Script for playing sound through lms.

Post by Skippiemanz »

Cool thanks! Maybe this can be combined wit TTS! I will have a go at it tonight!

*EDIT* Hmm to bad.. It couldn't find any of my players...

I have the LMS running on my synology NAS and us RpI with picore player as players..
User avatar
capman
Posts: 153
Joined: Friday 12 July 2013 20:48
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Belgium
Contact:

Re: Script for playing sound through lms.

Post by capman »

Maybe when you change in the script the players name to the name of the player where you want to play it.
In the script find "my $szPlayerName = "Keuken";" and change the name "Keuken' to the name of your player.
A have also LMS running on my NAS and on my Rpi a have max2play as player.
Skippiemanz
Posts: 230
Joined: Sunday 14 July 2013 20:21
Target OS: Linux
Domoticz version: 4.10233
Location: Alkmaar, The Netherlands
Contact:

Re: Script for playing sound through lms.

Post by Skippiemanz »

Thansk for your reply,]

I alreay changed that. I have 3 players, Badkamer, Keuken, Zolder but none of them work al with the same response:

Player with name: Badkamer could not be found!

I've set up the perl file with the following parameters

Code: Select all

# Parameter defaults overridden by command line
my $szServerIP      = "192.168.165.5";
my $iServerCLIPort  = 9002;
my $szPlayerName   = "Badkamer";
my $szLine1      = "MP3 Test";
my $szLine2      = "Afspelen Badkamer";
my $szSound      = "/home/pi/domoticz/scripts/music/deurbel/christmas.mp3";   # path on the machine where announcement.pl runs
my $user      = "";
my $passw      = "";
[/code
Skippiemanz
Posts: 230
Joined: Sunday 14 July 2013 20:21
Target OS: Linux
Domoticz version: 4.10233
Location: Alkmaar, The Netherlands
Contact:

Re: Script for playing sound through lms.

Post by Skippiemanz »

Thansk for your reply,]

I alreay changed that. I have 3 players, Badkamer, Keuken, Zolder but none of them work al with the same response:

Player with name: Badkamer could not be found!

I've set up the perl file with the following parameters

Code: Select all

# Parameter defaults overridden by command line
my $szServerIP      = "192.168.165.5";
my $iServerCLIPort  = 9002;
my $szPlayerName   = "Badkamer";
my $szLine1      = "MP3 Test";
my $szLine2      = "Afspelen Badkamer";
my $szSound      = "/home/pi/domoticz/scripts/music/deurbel/christmas.mp3";   # path on the machine where announcement.pl runs
my $user      = "";
my $passw      = "";
*EDIT* Found it! The CLI poort on Synology LMS is 9092 instead of the 9002 i assumed! It now works!

But only when the script is runned on the synology. When runned from my Rpi where Domoticz is on it only stops the playlist an after a few seconds it resumes.
User avatar
capman
Posts: 153
Joined: Friday 12 July 2013 20:48
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Belgium
Contact:

Re: Script for playing sound through lms.

Post by capman »

There's a package for domoticz running on a synology. I have now domoticz running from my synology , 24/7 , and it's very stable.
Beta: http://www.jadahl.com/domoticz_beta_6/
Stable: http://www.jadahl.com/domoticz_stable_6/
Skippiemanz
Posts: 230
Joined: Sunday 14 July 2013 20:21
Target OS: Linux
Domoticz version: 4.10233
Location: Alkmaar, The Netherlands
Contact:

Re: RE: Re: Script for playing sound through lms.

Post by Skippiemanz »

capman wrote:There's a package for domoticz running on a synology. I have now domoticz running from my synology , 24/7 , and it's very stable.
Beta: http://www.jadahl.com/domoticz_beta_6/
Stable: http://www.jadahl.com/domoticz_stable_6/
Thanks I know, had my domoticz running on the Nas for a while. But had some problem setting up some scripts on the Nas Witch where designed for rpi. I will look in tho the playing of remote files.
tottka
Posts: 19
Joined: Wednesday 19 July 2017 19:50
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: US
Contact:

Re: Script for playing sound through lms.

Post by tottka »

Can this script run on a rPi2? My max2play & LMS are on a different rPi than Domoticz. When I run the script the song playing pauses for a second or 2 then resumes. Also if the player is paused when the script runs, the paused song starts up.

thanks for the work you are doing on this
User avatar
philchillbill
Posts: 399
Joined: Monday 12 September 2016 13:47
Target OS: Linux
Domoticz version: beta
Location: Eindhoven. NL
Contact:

Re: Script for playing sound through lms.

Post by philchillbill »

I've written up an alternative approach to this in the wiki - might be useful for anybody having problems with the above script

https://www.domoticz.com/wiki/Logitech_ ... ox_Players
Alexa skills author: EvoControl, Statereport, MediaServer, LMS-lite
xavier1306
Posts: 10
Joined: Sunday 24 September 2017 23:19
Target OS: -
Domoticz version:
Contact:

Re: Script for playing sound through lms.

Post by xavier1306 »

philchillbill wrote:I've written up an alternative approach to this in the wiki - might be useful for anybody having problems with the above script

https://www.domoticz.com/wiki/Logitech_ ... ox_Players
I shall have asked directly to you then :)


It works fine with me, excellent job !!
2limitations:

- I have a too large delay between the moment the script is triggered and the real sound feedback (maybe 7-8 seconds)
Anything I can do with this ? Is there a kind of time delay in the perl script?

- sometimes randomly, the prechime is playing after the audio alert itself. Strange !?

Envoyé de mon MI 5s en utilisant Tapatalk

User avatar
philchillbill
Posts: 399
Joined: Monday 12 September 2016 13:47
Target OS: Linux
Domoticz version: beta
Location: Eindhoven. NL
Contact:

Re: Script for playing sound through lms.

Post by philchillbill »

Answered in the other thread where you posted this.


Sent from my iPhone using Tapatalk
Alexa skills author: EvoControl, Statereport, MediaServer, LMS-lite
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest