mercoledì 20 agosto 2014

Insegnante di canto per canarini

Oggi vi mostro un esempio di creare un software molto particolare per le nostre esigenze con Livecode.
Prima di tutto una premessa, i canarini imparano a cantare bene solo sentendo altri maschi. Per questo motivo il canarino della signora Jacqueline Landman-Gay, solo in tutta la casa, aveva bisogno di creare un programma che facesse sentire al suo canarino i vari cinguettii di altri canarini maschi.
Per prima cosa la signora ha scaricato degli MP3 contenenti i versi dei canarini, poi ha creato il programma con un elenco selezionabile contenente la lista degli MP3, il player e un pulsante:
Il codice del pulsante è il seguente:

on mouseUp
   if the label of me = "start" then
      set the label of me to "Stop"
      playSong
   else
      set the label of me to "Start"
      stopSongs
   end if
end mouseUp


Il resto del codice è tutto qui:

on preOpenCard
   put "" into fld "next play"
   set the hilitedlines of fld "playlist" to 0
   get songPath()
   set the directory to it
   put the files into tSongs
   filter tSongs with "*.mp3"
   if tSongs = "" then put "No songs found." into tSongs
   put tSongs into fld "playlist"
end preOpenCard

on closeCard
   stopSongs
end closeCard

on playSong
   -- randomly choose an mp3, play it at a random time
   put any line of fld "playlist" into tSongName
   put songPath() & tSongName into tSong
   lock messages
   set the filename of player "canary" to tSong
   unlock messages
   set the hilitedlines of fld "playlist" to lineoffset(tSongName,fld "playlist")
   set the currentTime of player "canary" to 1
   start player "canary"
end playSong

on stopSongs -- stop playback
   lock messages -- prevent "playstopped" from being sent
   stop player "canary"
   put "" into fld "next play"
   set the hilitedlines of fld "playlist" to 0
   repeat for each line tLine in the pendingmessages
      if tLine contains "playSong" then cancel (item 1 of tLine)
   end repeat
   unlock messages
end stopSongs

on playStopped pPlayer
   set the hilitedlines of fld "playlist" to 0
   if the label of btn "start" = "start" then pass playStopped -- not in auto mode
   put random(word 1 of the label of btn "Lag") into tSecs
   displayTime tSecs
   if "playSong" is not in the pendingmessages
   then send "playSong" to me in tSecs seconds
end playStopped

function songPath
   put the filename of this stack into tPath
   set the itemdel to slash
   put "songs" into last item of tPath
   return tPath & slash
end songPath

on displayTime pSecs
   get the long time
   convert it to seconds
   add pSecs to it
   convert it to long time
   put "Next song at:" && it into fld "next play"
end displayTime


Il messaggio preOpenCard serve a caricare tutte le musiche all'avvio.
Il messaggio playSong  serve ad avviare la musica scelta e mandarne un'altra a caso ogni intervallo stabilito.
Il messaggio stopSongs ferma l'esecuzione della musica.
Migliorando la grafica, il programma finale è il seguente:
Il programma può essere scaricato da qui: http://newsletters.livecode.com/july/issue29/Canary_Stack.zip
Tempo per ottenere il programma completo: 20 minuti! Con quale altro linguaggio di programmazione potevate ottenere lo stesso risultato con così poco tempo?

Nessun commento:

Posta un commento