Audio alarm

From Rosetta Code
Revision as of 17:06, 15 December 2013 by rosettacode>Jecxjo (Racket implementation)
Audio alarm is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

AudioAlarm is a program that asks the user to enter a certain number, representing a number of seconds. After the user enters the number, the program will ask the user to enter the name of an MP3 audio file (without the .mp3 file extension). The program will then display a (usually blank) page. After the time (indicated by the number) is up, a sound (indicated by the MP3 file) will go off. Useful for timers and alarm clocks. The app must be installed in its own folder, preferrably with a name like AudioAlarm. To install a sound on the app, just copy the MP3 to the app folder you set up. Then, when the app asks you for the filename, you just type in the name without an extension.

AutoHotkey

Uses Run for maximum compatibility <lang AHK>Inputbox, seconds, Seconds, Enter a number of seconds: FileSelectFile, File, 3, %A_ScriptDir%, File to be played, MP3 Files (*.mp3) Sleep Seconds*1000 RunWait % file</lang>

JavaScript/HTML

<lang JavaScript><title> AudioAlarm </title> <script>

   var a=prompt("Enter a number of seconds", "");
   var b=prompt("Enter the name of an MP3 file you have installed in the directory (without extension)", "");
   document.write("<meta http-equiv='refresh' content='"+a+";url="+b+".mp3'>")

</script></lang>

Liberty BASIC

LB can play wav files natively. Here we call the standard Windows Media Player for an MP3. If not already running, this will add an extra delay... It will error if the mp3 file does not exist in the specified path. <lang lb>nomainwin

prompt "Delay in seconds"; sec$ prompt "MP3 to play as alarm"; mp3$ f$ ="f:\"; mp3$; ".mp3"

timer val( sec$) *100, [done] wait

[done] timer 0 run "C:\Program Files\Windows Media Player\wmplayer.exe " +chr$(34) +f$ +chr$(34)

end</lang>

Racket

Racket does not currently have native mp3 support so this example uses system to call an external application. <lang racket>#lang racket (display "Time to wait in seconds: ") (define time (string->number (read-line)))

(display "File Name: ") (define file-name (read-line))

(when (file-exists? (string->path (string-append file-name ".mp3")))

 (sleep time)
 (system* "/usr/bin/mpg123" (string-append file-name ".mp3")))</lang>

REXX

using SLEEP

<lang rexx>/*REXX pgm to prompt user for: # (of secs); a name of a MP3 file to play*/

say '──────── Please enter a number of seconds to wait:' parse pull waitTime .

                        /*add code to verify number is a valid number. */

say '──────── Please enter a name of an MP3 file to play:' parse pull MP3FILE

                       /*add code to verify answer is a valid filename.*/

call sleep waitTime MP3FILE'.MP3'

                                      /*stick a fork in it, we're done.*/</lang>

output when using the input of: xxx

using spin

<lang rexx>/*REXX pgm to prompt user for: # (of secs); a name of a MP3 file to play*/

say '──────── Please enter a number of seconds to wait:' parse pull waitTime .

say '──────── Please enter a name of an MP3 file to play:' parse pull MP3FILE

call time 'Reset' /*reset the REXX (elapsed) timer.*/

  do  until time('E')  >waitTime      /*wait out the clock (in seconds)*/
  end

MP3FILE'.MP3'

                                      /*stick a fork in it, we're done.*/</lang>

Tcl

Library: Snack

<lang tcl>package require sound

fconfigure stdout -buffering none puts -nonewline "How long to wait for (seconds): " gets stdin delay puts -nonewline "What file to play: " gets stdin soundFile

snack::sound snd snd read $soundFile after [expr {$delay * 1000}] {snd play -command {set done 1}} vwait done

catch {snd stop} snd destroy puts "all done" exit</lang>