Audio alarm: Difference between revisions

From Rosetta Code
Content added Content deleted
(Racket implementation)
Line 33: Line 33:


end</lang>
end</lang>

=={{header|Pure Data}}==
(Pure Data really needed file upload: A screenshot of the patch would save space and at the same time be self-explanatory, thus making the script superfluous.)

'''Alarm.pd:'''
<pre>
#N canvas 623 244 351 664 10;
#X obj 18 19 cnv 15 310 30 empty empty empty 20 12 0 14 -203904 -66577 0;
#X obj 227 183 cnv 15 60 30 empty empty empty 20 12 0 14 -204786 -66577 0;
#X obj 18 433 cnv 15 310 36 empty empty empty 20 12 0 14 -203904 -66577 0;
#X floatatom 155 27 5 0 24 1 : h -;
#X floatatom 197 27 5 0 59 1 : - -;
#X floatatom 239 27 5 0 59 1 hh:mm:ss - -;
#X obj 155 63 * 3600;
#X obj 197 63 * 60;
#X obj 209 340 moses 1;
#X msg 209 375 Alarm;
#X obj 209 397 print;
#X floatatom 77 27 8 0 90000 0 duration duration -;
#X obj 155 107 +;
#X obj 200 129 +;
#X obj 239 63 change;
#X msg 239 85 bang;
#X obj 239 107 s h;
#X obj 197 85 int;
#X obj 35 293 metro 1000;
#X obj 35 340 int;
#X obj 75 340 - 1;
#X obj 35 196 bng 15 250 50 0 empty empty start -7 -7 0 10 -204786 -1 -13381;
#X msg 98 246 stop;
#X obj 98 196 bng 15 250 50 0 empty pause pause -7 -7 0 10 -261234 -1 -86277;
#X obj 158 196 bng 15 250 50 0 empty empty resume -10 -7 0 10 -262130 -1 -83269;
#X obj 62 246 int;
#X obj 100 375 > 0;
#X obj 158 246 int;
#X obj 200 152 s duration;
#X obj 35 216 t b b;
#X text 133 28 <-;
#X floatatom 231 196 8 0 0 2 countdown countdown_in countdown_out;
#X obj 35 397 s countdown_in;
#X obj 209 318 r countdown_out;
#X obj 36 484 openpanel;
#X obj 129 575 readsf~;
#X obj 162 626 dac~;
#X msg 129 506 1;
#X obj 246 565 loadbang;
#X obj 36 449 bng 15 250 50 0 empty select select -10 -7 0 10 -262130 -1 -1;
#X obj 122 449 bng 15 250 50 0 empty empty test -4 -7 0 10 -204786 -1 -1;
#X obj 181 449 hsl 100 15 0 1 0 0 empty volume volume -2 -7 0 10 -261682 -1 -1 4950 0;
#X obj 163 600 *~;
#X text 72 438 ALARM;
#X text 72 449 SOUND;
#X obj 129 484 t b b;
#X msg 36 506 set open \$1;
#X obj 294 449 bng 15 250 50 0 empty empty off -1 -7 0 10 -261234 -1 -1;
#X msg 294 506 0;
#X msg 246 587 \; pd dsp 1 \; volume 0.5 \; select 1;
#X msg 36 528 open *.wav;
#X connect 3 0 6 0;
#X connect 4 0 7 0;
#X connect 4 0 14 0;
#X connect 5 0 13 1;
#X connect 5 0 14 0;
#X connect 6 0 12 0;
#X connect 7 0 17 0;
#X connect 8 0 9 0;
#X connect 8 0 22 0;
#X connect 8 0 45 0;
#X connect 9 0 10 0;
#X connect 11 0 25 1;
#X connect 12 0 13 0;
#X connect 13 0 28 0;
#X connect 14 0 15 0;
#X connect 15 0 16 0;
#X connect 17 0 12 1;
#X connect 18 0 19 0;
#X connect 19 0 20 0;
#X connect 19 0 26 0;
#X connect 19 0 32 0;
#X connect 20 0 19 1;
#X connect 21 0 29 0;
#X connect 22 0 18 0;
#X connect 23 0 22 0;
#X connect 24 0 27 0;
#X connect 25 0 19 1;
#X connect 26 0 27 1;
#X connect 27 0 18 0;
#X connect 29 0 18 0;
#X connect 29 1 25 0;
#X connect 33 0 8 0;
#X connect 34 0 46 0;
#X connect 35 0 42 0;
#X connect 35 1 45 0;
#X connect 37 0 35 0;
#X connect 38 0 49 0;
#X connect 39 0 34 0;
#X connect 40 0 45 0;
#X connect 41 0 42 1;
#X connect 42 0 36 0;
#X connect 42 0 36 1;
#X connect 45 0 37 0;
#X connect 45 1 50 0;
#X connect 46 0 50 0;
#X connect 47 0 48 0;
#X connect 48 0 35 0;
#X connect 50 0 35 0;
</pre>
'''Features:'''
* Pd has built-in WAV file support - mp3 will require an extension
* Time input by hours, minutes and seconds
* Start, pause and resume button
* Sound file selection via file browser dialog, test button and volume control


=={{header|Racket}}==
=={{header|Racket}}==

Revision as of 11:53, 30 January 2014

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>

Pure Data

(Pure Data really needed file upload: A screenshot of the patch would save space and at the same time be self-explanatory, thus making the script superfluous.)

Alarm.pd:

#N canvas 623 244 351 664 10;
#X obj 18 19 cnv 15 310 30 empty empty empty 20 12 0 14 -203904 -66577 0;
#X obj 227 183 cnv 15 60 30 empty empty empty 20 12 0 14 -204786 -66577 0;
#X obj 18 433 cnv 15 310 36 empty empty empty 20 12 0 14 -203904 -66577 0;
#X floatatom 155 27 5 0 24 1 : h -;
#X floatatom 197 27 5 0 59 1 : - -;
#X floatatom 239 27 5 0 59 1 hh:mm:ss - -;
#X obj 155 63 * 3600;
#X obj 197 63 * 60;
#X obj 209 340 moses 1;
#X msg 209 375 Alarm;
#X obj 209 397 print;
#X floatatom 77 27 8 0 90000 0 duration duration -;
#X obj 155 107 +;
#X obj 200 129 +;
#X obj 239 63 change;
#X msg 239 85 bang;
#X obj 239 107 s h;
#X obj 197 85 int;
#X obj 35 293 metro 1000;
#X obj 35 340 int;
#X obj 75 340 - 1;
#X obj 35 196 bng 15 250 50 0 empty empty start -7 -7 0 10 -204786 -1 -13381;
#X msg 98 246 stop;
#X obj 98 196 bng 15 250 50 0 empty pause pause -7 -7 0 10 -261234 -1 -86277;
#X obj 158 196 bng 15 250 50 0 empty empty resume -10 -7 0 10 -262130 -1 -83269;
#X obj 62 246 int;
#X obj 100 375 > 0;
#X obj 158 246 int;
#X obj 200 152 s duration;
#X obj 35 216 t b b;
#X text 133 28 <-;
#X floatatom 231 196 8 0 0 2 countdown countdown_in countdown_out;
#X obj 35 397 s countdown_in;
#X obj 209 318 r countdown_out;
#X obj 36 484 openpanel;
#X obj 129 575 readsf~;
#X obj 162 626 dac~;
#X msg 129 506 1;
#X obj 246 565 loadbang;
#X obj 36 449 bng 15 250 50 0 empty select select -10 -7 0 10 -262130 -1 -1;
#X obj 122 449 bng 15 250 50 0 empty empty test -4 -7 0 10 -204786 -1 -1;
#X obj 181 449 hsl 100 15 0 1 0 0 empty volume volume -2 -7 0 10 -261682 -1 -1 4950 0;
#X obj 163 600 *~;
#X text 72 438 ALARM;
#X text 72 449 SOUND;
#X obj 129 484 t b b;
#X msg 36 506 set open \$1;
#X obj 294 449 bng 15 250 50 0 empty empty off -1 -7 0 10 -261234 -1 -1;
#X msg 294 506 0;
#X msg 246 587 \; pd dsp 1 \; volume 0.5 \; select 1;
#X msg 36 528 open *.wav;
#X connect 3 0 6 0;
#X connect 4 0 7 0;
#X connect 4 0 14 0;
#X connect 5 0 13 1;
#X connect 5 0 14 0;
#X connect 6 0 12 0;
#X connect 7 0 17 0;
#X connect 8 0 9 0;
#X connect 8 0 22 0;
#X connect 8 0 45 0;
#X connect 9 0 10 0;
#X connect 11 0 25 1;
#X connect 12 0 13 0;
#X connect 13 0 28 0;
#X connect 14 0 15 0;
#X connect 15 0 16 0;
#X connect 17 0 12 1;
#X connect 18 0 19 0;
#X connect 19 0 20 0;
#X connect 19 0 26 0;
#X connect 19 0 32 0;
#X connect 20 0 19 1;
#X connect 21 0 29 0;
#X connect 22 0 18 0;
#X connect 23 0 22 0;
#X connect 24 0 27 0;
#X connect 25 0 19 1;
#X connect 26 0 27 1;
#X connect 27 0 18 0;
#X connect 29 0 18 0;
#X connect 29 1 25 0;
#X connect 33 0 8 0;
#X connect 34 0 46 0;
#X connect 35 0 42 0;
#X connect 35 1 45 0;
#X connect 37 0 35 0;
#X connect 38 0 49 0;
#X connect 39 0 34 0;
#X connect 40 0 45 0;
#X connect 41 0 42 1;
#X connect 42 0 36 0;
#X connect 42 0 36 1;
#X connect 45 0 37 0;
#X connect 45 1 50 0;
#X connect 46 0 50 0;
#X connect 47 0 48 0;
#X connect 48 0 35 0;
#X connect 50 0 35 0;

Features:

  • Pd has built-in WAV file support - mp3 will require an extension
  • Time input by hours, minutes and seconds
  • Start, pause and resume button
  • Sound file selection via file browser dialog, test button and volume control

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>