1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2013 Alexis Laferrière <alexis.laf@xymus.net>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Client for a MPD server
22 # Connection to a MPD server
25 # Socket connection to server.
26 var socket
: nullable Socket = null
35 var password
: nullable String
38 var error
: nullable String = null
40 # Connect to the MPD server
43 var p
: nullable Socket = null
45 p
= new Socket.client
(host
, port
)
49 var rep
= p
.read
(1024)
50 assert not rep
.is_empty
51 if not rep
.has_prefix
("OK") then
52 print
"MPD responded {rep}"
58 var password
= password
59 if password
!= null then
60 write_and_check
("password {password}\n")
64 # Write a command to the MPD server
65 protected fun write_and_check
(msg
: String)
67 if socket
== null then connect
71 var rep
= socket
.read
(1024)
72 if not rep
.has_prefix
("OK") then
73 print
"Error: MPD responded {rep}"
79 # Get MPD server status
80 fun status
: nullable ServerStatus
82 if socket
== null then connect
84 var volume
: nullable Int = null
85 var state
: nullable String = null
86 var elapsed
: nullable Int = null
87 var time_at
: nullable Int = null
88 var time_total
: nullable Int = null
91 socket
.write
("status\n")
92 var rep
= socket
.read
(1024)
93 for l
in rep
.split_with
("\n") do
94 var words
= l
.split_with
(" ")
95 if words
.length
> 1 then
96 var key
= words
[0].to_lower
97 var first_space
= l
.chars
.index_of
(' ')
98 var rest
= l
.substring_from
(first_space
+1)
99 if key
== "volume:" then
101 if volume
== -1 then volume
= null
102 else if key
== "state:" then
104 else if key
== "elapsed:" then
106 else if key
== "time:" then
107 var times
= rest
.split
(":")
108 time_at
= times
[0].to_i
109 time_total
= times
[1].to_i
114 if state
!= null then
115 var status
= new ServerStatus(volume
, state
, elapsed
, time_at
, time_total
)
122 # Set the volume relatively
123 fun relative_volume
=(diff
: Int)
125 if socket
== null then connect
128 if status
!= null then
129 var vol
= status
.volume
131 var new_vol
= vol
+ diff
132 new_vol
= new_vol
.max
(0).min
(100)
138 error
= "Cannot get volume"
141 # Set MPD server volume.
142 fun volume
=(vol
: Int) do write_and_check
("setvol {vol}\n")
144 # Pause music playing on the MPD server
145 fun pause
do write_and_check
("pause\n")
147 # Stop music playing on the MPD server
148 fun stop
do write_and_check
("stop\n")
150 # Play music playing on the MPD server
151 fun play
do write_and_check
("play\n")
153 # Get information on the currently playing song on the MPD server
154 fun current_song
: nullable SongInfo
156 if socket
== null then connect
158 var album
: nullable String = null
159 var artist
: nullable String = null
160 var title
: nullable String = null
161 var time
: nullable Int = null
163 socket
.write
("currentsong\n")
164 var rep
= socket
.read
(1024)
165 for l
in rep
.split_with
("\n") do
166 var words
= l
.split_with
(" ")
167 if words
.length
> 1 then
168 var key
= words
[0].to_lower
169 var first_space
= l
.chars
.index_of
(' ')
170 var rest
= l
.substring_from
(first_space
+1)
171 if key
== "album:" then
173 else if key
== "artist:" then
175 else if key
== "title:" then
177 else if key
== "time:" then
183 if album
!= null and artist
!= null and
184 title
!= null and time
!= null then
185 return new SongInfo(album
, artist
, title
, time
)
191 # Load playlist named `name`.
192 fun load_playlist
(name
: String)
194 write_and_check
"load \"{name}\
""
209 # Song total duration.
217 var volume
: nullable Int
219 # Playback state (play/stop/pause).
222 # Is MPD server playing?
223 fun playing
: Bool do return state
== "play"
225 # Is MPD server stopped?
226 fun stopped
: Bool do return state
== "stop"
228 # Time elapsed within the current song.
229 var elapsed
: nullable Int
232 var time_at
: nullable Int
234 # Total time of the current song.
235 var time_total
: nullable Int
237 # Get the cursor position on the song duration.
238 fun time_ratio
: nullable Float do
239 if time_at
== null or time_total
== null then return null
240 return time_at
.to_f
/ time_total
.to_f
245 if volume
!= null then vol
= volume
.to_s
247 var time_at
= time_at
248 var time_total
= time_total
249 var elapsed
= elapsed
250 if time_at
!= null and time_total
!= null and elapsed
!= null then
251 return "volume: {vol}\nstate: {state}\nelapsed: {elapsed}\ntime_[at|total]: {time_at}:{time_total}\ntime_ratio: {time_ratio.as(not null)}"
253 return "volume: {vol}\nstate: {state}"