Play recorded sounds: Difference between revisions
(Added PicoLisp) |
(Added R code) |
||
Line 109: | Line 109: | ||
;suitable for 2D games and music playing. |
;suitable for 2D games and music playing. |
||
; TODO: There is a Sound3D library for 3D Games, needs to be decribed here too</lang> |
; TODO: There is a Sound3D library for 3D Games, needs to be decribed here too</lang> |
||
=={{header|R}}== |
|||
R's sound package uses system commands to call a builtin media player. On windows, by default, this is Media Player. You can see the system call with <code>WavPlayer()</code>. Only .WAV files are supported, and the external code call means there is some latency before sounds are played. Samples longer than 10 minutes may correspond to significant chunks of your machine's memory. |
|||
<lang r> |
|||
#Setup |
|||
# Warning: The example files are Windows specific. |
|||
library(sound) |
|||
media_dir <- file.path(Sys.getenv("SYSTEMROOT"), "Media") |
|||
chimes <- loadSample(file.path(media_dir, "chimes.wav")) |
|||
chord <- loadSample(file.path(media_dir, "chord.wav")) |
|||
# Play sequentially |
|||
play(appendSample(chimes, chord)) |
|||
# Play simultaneously |
|||
play(chimes + chord) |
|||
# Stopping before the end |
|||
play(cutSample(chimes, 0, 0.2)) |
|||
#Looping |
|||
for(i in 1:3) play(chimes) #leaves a gap between instances |
|||
three_chimes <- lapply(vector(3, mode = "list"), function(x) chimes) |
|||
play(do.call(appendSample, three_chimes)) #no gap, see also cutSampleEnds |
|||
# Volume control |
|||
play(3 * chimes) |
|||
#Stereo effect |
|||
play(stereo(chimes, chord)) |
|||
#Other actions (not obviously possible) |
|||
</lang> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
Revision as of 15:41, 2 July 2010
You are encouraged to solve this task according to the task description, using any language you may know.
Load at least two prerecorded sounds, and demonstrate as many of these features as you can:
- playing them individually and simultaneously
- stopping before the end of the sound
- looping (preferably glitch-free)
- setting the volume of each sound
- stereo or 3D positional mixing
- performing other actions when marked times in the sound arrive
Describe:
- The supported audio formats, briefly.
- Whether it is suitable for game sound effects (low-latency start, resource efficiency, supports many simultaneous sounds, etc.)
- Whether it is suitable for playing music (long duration ).
[Note: If it seems to be a good idea, this task may be revised to specify a particular timeline rather than just 'demonstrate these features'.]
Where applicable, please categorize examples primarily by the audio facility used (library/API/program/platform) rather than the language if the language is incidental (e.g. "Mac OS X CoreAudio" or "mplayer" rather than "C" or "bash").
AutoHotkey
No builtin functions for:
1. monitoring timepoints in the sound
2. simultaneous play
<lang AutoHotkey>SoundPlay, %A_WinDir%\Media\tada.wav, wait
SoundPlay, %A_WinDir%\Media\Windows XP Startup.wav, wait
- simulaneous play may require a second script
SoundPlay, %A_WinDir%\Media\tada.wav SoundPlay, Nonexistent ; stop before finishing
SoundSet +10 ; increase volume by 10% Loop, 2
SoundPlay, %A_WinDir%\Media\tada.wav, wait</lang>
C#
<lang csharp>using System; using System.Threading; using System.Media;
class Program {
static void Main(string[] args) { //load sound file SoundPlayer s1 = new SoundPlayer(); // s1.SoundLocation = file; // or "s1 = new SoundPlayer(file)"
//play s1.Play();
//play for 0.1 seconds s1.Play(); Thread.Sleep(100); s1.Stop();
//loops s1.PlayLooping(); }
}</lang>
PicoLisp
The obvious way is to call 'sox', the "Swiss Army knife of audio manipulation" (man sox).
The following plays two files "a.wav" and "b.wav" simultaneously (to play them individually, omit the "-m" flag). The first one is played with a volume of 75 percent, the second with 25 percent, starting at the 4th second, with a duration of 6 seconds, looping 5 times. <lang PicoLisp>(call 'sox
"-m" "-v" "0.75" "a.wav" "-v" "0.25" "b.wav" "-d" "trim" 4 6 "repeat" 5 )</lang>
PureBasic
<lang PureBasic>InitSound()
- We need this to use Sound functions
UseOGGSoundDecoder()
- Now we can not only load wav sound files, but also ogg encoded ones.
- With movie libary more formats can be played (depends on system) but you cannot
- handle them with Sound functions
If Not LoadSound(1,"Path/to/Sound/1.ogg") Or Not LoadSound(2,"Path/to/Sound/2.wav") MessageRequester("Error","One of our sounds could not be loaded"+Chr(10)+"Use Debugger to check which one") EndIf
- - simultaneous playing
PlaySound(1) PlaySound(2)
- - manipulating sounds
Delay(1000)
- pause for one second, to let user hear something
SoundVolume(1,90) SoundVolume(2,60)
- reduce volume of the sounds a bit
SoundPan(1,-80) SoundPan(2,100)
- Sound 1 mostly left speaker, sound 2 only right speaker
SoundFrequency(1,30000)
- play sound one faster
Delay(1000)
- pause for one second, to let user hear effects of previous actions
- - stopping while playing
StopSound(-1)
- value -1 stops all playing sounds
PlaySound(1,#PB_Sound_Loop)
- continous looping without glitch
- suitable for 2D games and music playing.
- TODO
- There is a Sound3D library for 3D Games, needs to be decribed here too</lang>
R
R's sound package uses system commands to call a builtin media player. On windows, by default, this is Media Player. You can see the system call with WavPlayer()
. Only .WAV files are supported, and the external code call means there is some latency before sounds are played. Samples longer than 10 minutes may correspond to significant chunks of your machine's memory.
<lang r>
- Setup
- Warning: The example files are Windows specific.
library(sound) media_dir <- file.path(Sys.getenv("SYSTEMROOT"), "Media") chimes <- loadSample(file.path(media_dir, "chimes.wav")) chord <- loadSample(file.path(media_dir, "chord.wav"))
- Play sequentially
play(appendSample(chimes, chord))
- Play simultaneously
play(chimes + chord)
- Stopping before the end
play(cutSample(chimes, 0, 0.2))
- Looping
for(i in 1:3) play(chimes) #leaves a gap between instances
three_chimes <- lapply(vector(3, mode = "list"), function(x) chimes) play(do.call(appendSample, three_chimes)) #no gap, see also cutSampleEnds
- Volume control
play(3 * chimes)
- Stereo effect
play(stereo(chimes, chord))
- Other actions (not obviously possible)
</lang>
Ruby
There aren't many mature sound libraries for Ruby. The
package win32-sound can play WAV files on the Windows platform only.
<lang ruby>require 'win32/sound' include Win32
sound1 = ENV['WINDIR'] + '\Media\Windows XP Startup.wav' sound2 = ENV['WINDIR'] + '\Media\Windows XP Shutdown.wav'
puts "play the sounds sequentially" [sound1, sound2].each do |s|
t1 = Time.now Sound.play(s) puts "'#{s}' duration: #{(Time.now.to_f - t1.to_f)} seconds"
end
puts "attempt to play the sounds simultaneously" [sound1, sound2].each {|s| Sound.play(s, Sound::ASYNC)}
puts <<END the above only plays the second sound2 because the library only appears to be able to play one sound at a time. END
puts "loop a sound for a few seconds" puts Time.now Sound.play(sound1, Sound::ASYNC + Sound::LOOP) sleep 10 Sound.stop puts Time.now
puts "manipulate the volume" vol_left, vol_right = Sound.wave_volume Sound.play(sound1, Sound::ASYNC) sleep 1 puts "right channel quiet" Sound.set_wave_volume(vol_left, 0) sleep 1 puts "left channel quiet" Sound.set_wave_volume(0, vol_right) sleep 1 puts "restore volume" Sound.set_wave_volume(vol_left, vol_right)
sleep 1 puts "the asynchronous sound is cancelled when the program exits"</lang>
Tcl
<lang tcl>package require sound
- Potentially also require driver support for particular formats
- Load some sounds in; this part can be slow
snack::sound s1 s1 read $soundFile1 snack::sound s2 s2 read $soundFile2
- Play a sound for a while (0.1 seconds; this is a low-latency operation)
s1 play after 100 set done 1; vwait done; # Run the event loop for a while s1 stop
- Play two sounds at once (for 30 seconds) while mixing together
s1 play s2 play after 30000 set done 1; vwait done s1 stop s2 stop</lang> Note that this library is capable of handling both short and long sounds (sound effects and music).
UNIX Shell
<lang bash>#!/usr/bin/sh
- play.sh
- Plays .au files.
- Usage: play.sh <recorded_sound.au>
cat $1 >> /dev/audio # Write file $1 to the speaker's Character Special (/dev/audio).</lang>