Play recorded sounds: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
Line 20: Line 20:
This snippet of code will stream an unsigned 8-bit PCM sample to the [[wp:Yamaha_YM2612|Yamaha 2612's]] [[wp:Digital-to-analog_converter|DAC]]. Unfortunately, the data needs to be continuously streamed, meaning that the game essentially comes to a halt while this is happening. However, a clever game designer can hide this fact quite easily by only using voice samples at key moments where the player expects a brief pause in the action. Playing with other sounds is possible if the Z80 coprocessor is handling the playback, however the DAC uses a few of the channels and therefore some of them will not be heard if DAC streaming begins during regular FM audio playback.
This snippet of code will stream an unsigned 8-bit PCM sample to the [[wp:Yamaha_YM2612|Yamaha 2612's]] [[wp:Digital-to-analog_converter|DAC]]. Unfortunately, the data needs to be continuously streamed, meaning that the game essentially comes to a halt while this is happening. However, a clever game designer can hide this fact quite easily by only using voice samples at key moments where the player expects a brief pause in the action. Playing with other sounds is possible if the Z80 coprocessor is handling the playback, however the DAC uses a few of the channels and therefore some of them will not be heard if DAC streaming begins during regular FM audio playback.


<lang 68000devpac>dac_data equ $2A
<syntaxhighlight lang="68000devpac">dac_data equ $2A
dac_enable equ $2B
dac_enable equ $2B
LEA pcmsample,a1
LEA pcmsample,a1
Line 64: Line 64:


pcmSample:
pcmSample:
incbin "X:\ResAll\pcmSample.bin"</lang>
incbin "X:\ResAll\pcmSample.bin"</syntaxhighlight>


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
Line 70: Line 70:
1. monitoring timepoints in the sound <br>
1. monitoring timepoints in the sound <br>
2. simultaneous play
2. simultaneous play
<lang AutoHotkey>SoundPlay, %A_WinDir%\Media\tada.wav, wait
<syntaxhighlight lang="autohotkey">SoundPlay, %A_WinDir%\Media\tada.wav, wait
SoundPlay, %A_WinDir%\Media\Windows XP Startup.wav, wait
SoundPlay, %A_WinDir%\Media\Windows XP Startup.wav, wait


Line 80: Line 80:
SoundSet +10 ; increase volume by 10%
SoundSet +10 ; increase volume by 10%
Loop, 2
Loop, 2
SoundPlay, %A_WinDir%\Media\tada.wav, wait</lang>
SoundPlay, %A_WinDir%\Media\tada.wav, wait</syntaxhighlight>


=={{header|Batch File}}==
=={{header|Batch File}}==
Line 102: Line 102:
{{works with|BBC BASIC for Windows}}
{{works with|BBC BASIC for Windows}}
BBC BASIC for Windows has native support for playing MIDI files, and WAV files can be played using simple API calls:
BBC BASIC for Windows has native support for playing MIDI files, and WAV files can be played using simple API calls:
<lang bbcbasic> SND_LOOP = 8
<syntaxhighlight lang="bbcbasic"> SND_LOOP = 8
SND_ASYNC = 1
SND_ASYNC = 1
SND_FILENAME = &20000
SND_FILENAME = &20000
Line 142: Line 142:
PRINT "Stopped MIDI."
PRINT "Stopped MIDI."
END</lang>
END</syntaxhighlight>


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==


<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Threading;
using System.Threading;
using System.Media;
using System.Media;
Line 169: Line 169:
s1.PlayLooping();
s1.PlayLooping();
}
}
}</lang>
}</syntaxhighlight>


=={{header|Delphi}}==
=={{header|Delphi}}==
<lang Delphi>program PlayRecordedSounds;
<syntaxhighlight lang="delphi">program PlayRecordedSounds;


{$APPTYPE CONSOLE}
{$APPTYPE CONSOLE}
Line 180: Line 180:
begin
begin
sndPlaySound('SoundFile.wav', SND_NODEFAULT OR SND_ASYNC);
sndPlaySound('SoundFile.wav', SND_NODEFAULT OR SND_ASYNC);
end.</lang>
end.</syntaxhighlight>


=={{header|Go}}==
=={{header|Go}}==
Line 190: Line 190:


See the PicoLisp entry for a description of what these particular arguments do.
See the PicoLisp entry for a description of what these particular arguments do.
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 211: Line 211:
log.Fatal(err)
log.Fatal(err)
}
}
}</lang>
}</syntaxhighlight>


=={{header|GUISS}}==
=={{header|GUISS}}==
Line 217: Line 217:
Here we use the media player to play a sound file in the default directory:
Here we use the media player to play a sound file in the default directory:


<lang guiss>Start,Programs,Accessories,Media Player,Menu:File,Open,
<syntaxhighlight lang="guiss">Start,Programs,Accessories,Media Player,Menu:File,Open,
Doubleclick:Icon:Sound.WAV,Button:OK,Button:Play</lang>
Doubleclick:Icon:Sound.WAV,Button:OK,Button:Play</syntaxhighlight>


=={{header|Liberty BASIC}}==
=={{header|Liberty BASIC}}==
<lang lb>'Supports .mid and .wav natively
<syntaxhighlight lang="lb">'Supports .mid and .wav natively
'Midi may be played simultaneously
'Midi may be played simultaneously
'with .wav but only one .wav voice
'with .wav but only one .wav voice
Line 262: Line 262:
calldll #winmm, "waveOutSetVolume", 0 as long, _
calldll #winmm, "waveOutSetVolume", 0 as long, _
dwVol as long, ret as long
dwVol as long, ret as long
</lang>
</syntaxhighlight>




Line 270: Line 270:
This makes a good music player. There is a slight latency to load the player, so very low game
This makes a good music player. There is a slight latency to load the player, so very low game
latencies would not be well supported.
latencies would not be well supported.
<lang julia>using Distributed, Gtk, LibSndFile, MP3
<syntaxhighlight lang="julia">using Distributed, Gtk, LibSndFile, MP3
using FileIO: load
using FileIO: load


Line 407: Line 407:


recordingplayerapp()
recordingplayerapp()
</syntaxhighlight>
</lang>


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Line 414: Line 414:
stopping before the end of the sound,looping (preferably glitch-free)and setting the volume of each sound
stopping before the end of the sound,looping (preferably glitch-free)and setting the volume of each sound


<lang Mathematica>a = Import["sound1.flac","FLAC"]; b = Import["sound2.flac","FLAC"];
<syntaxhighlight lang="mathematica">a = Import["sound1.flac","FLAC"]; b = Import["sound2.flac","FLAC"];


ListPlay[a, {t, 0, 10}]; ListPlay[b, {t, 0, 10}];
ListPlay[a, {t, 0, 10}]; ListPlay[b, {t, 0, 10}];
Line 424: Line 424:
While[True,ListPlay[{a,b}, {t, 0, 10}];]
While[True,ListPlay[{a,b}, {t, 0, 10}];]


ListPlay[{0.5*a, 0.3*b}, {t, 0, 10}];</lang>
ListPlay[{0.5*a, 0.3*b}, {t, 0, 10}];</syntaxhighlight>


-Supported audio formats :
-Supported audio formats :
Line 445: Line 445:
{{trans|Go}}
{{trans|Go}}
Using "sox" to play two sound files mixed. These are "wav" files but "sox" recognizes a lot of sound formats.
Using "sox" to play two sound files mixed. These are "wav" files but "sox" recognizes a lot of sound formats.
<lang Nim>import osproc
<syntaxhighlight lang="nim">import osproc


let args = ["-m", "-v", "0.75", "a.wav", "-v", "0.25", "b.wav",
let args = ["-m", "-v", "0.75", "a.wav", "-v", "0.25", "b.wav",
Line 451: Line 451:
"trim", "4", "6",
"trim", "4", "6",
"repeat", "5"]
"repeat", "5"]
echo execProcess("sox", args = args, options = {poStdErrToStdOut, poUsePath})</lang>
echo execProcess("sox", args = args, options = {poStdErrToStdOut, poUsePath})</syntaxhighlight>


=={{header|Phix}}==
=={{header|Phix}}==
<lang Phix>integer xPlaySound = 0
<syntaxhighlight lang="phix">integer xPlaySound = 0


procedure play_sound()--string filename)
procedure play_sound()--string filename)
Line 469: Line 469:
end if
end if
end procedure
end procedure
play_sound()</lang>
play_sound()</syntaxhighlight>


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
Line 479: Line 479:
percent, the second with 25 percent, starting at the 4th second, with a duration
percent, the second with 25 percent, starting at the 4th second, with a duration
of 6 seconds, looping 5 times.
of 6 seconds, looping 5 times.
<lang PicoLisp>(call 'sox
<syntaxhighlight lang="picolisp">(call 'sox
"-m" "-v" "0.75" "a.wav" "-v" "0.25" "b.wav"
"-m" "-v" "0.75" "a.wav" "-v" "0.25" "b.wav"
"-d"
"-d"
"trim" 4 6
"trim" 4 6
"repeat" 5 )</lang>
"repeat" 5 )</syntaxhighlight>


=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang PureBasic>InitSound()
<syntaxhighlight lang="purebasic">InitSound()
; We need this to use Sound functions
; We need this to use Sound functions
UseOGGSoundDecoder()
UseOGGSoundDecoder()
Line 521: Line 521:


;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</syntaxhighlight>


=={{header|Python}}==
=={{header|Python}}==
Line 529: Line 529:
Pygame is a library for cross-platform game development and depends on the SDL multimedia library (SDL_mixer) for its audio playback. SDL_mixer supports any number of simultaneously playing channels of 16 bit stereo audio, plus a single channel of music, mixed by the popular MikMod MOD, Timidity MIDI, Ogg Vorbis, and SMPEG MP3 libraries.
Pygame is a library for cross-platform game development and depends on the SDL multimedia library (SDL_mixer) for its audio playback. SDL_mixer supports any number of simultaneously playing channels of 16 bit stereo audio, plus a single channel of music, mixed by the popular MikMod MOD, Timidity MIDI, Ogg Vorbis, and SMPEG MP3 libraries.


<lang python>import time
<syntaxhighlight lang="python">import time
from pygame import mixer
from pygame import mixer


Line 556: Line 556:
s1.stop()
s1.stop()
s2.stop()
s2.stop()
mixer.quit()</lang>
mixer.quit()</syntaxhighlight>


To play back .mp3 (or .ogg) files, the music import is used.
To play back .mp3 (or .ogg) files, the music import is used.


<lang python>import time
<syntaxhighlight lang="python">import time
from pygame import mixer
from pygame import mixer
from pygame.mixer import music
from pygame.mixer import music
Line 571: Line 571:


music.stop()
music.stop()
mixer.quit()</lang>
mixer.quit()</syntaxhighlight>


=={{header|R}}==
=={{header|R}}==
Line 578: Line 578:
R's sound package uses system commands to call a built-in 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.
R's sound package uses system commands to call a built-in 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.


<syntaxhighlight lang="r">
<lang r>
#Setup
#Setup
# Warning: The example files are Windows specific.
# Warning: The example files are Windows specific.
Line 608: Line 608:


#Other actions (not obviously possible)
#Other actions (not obviously possible)
</syntaxhighlight>
</lang>


=={{header|Racket}}==
=={{header|Racket}}==
(Works on all platforms.)
(Works on all platforms.)
<lang racket>
<syntaxhighlight lang="racket">
#lang racket/gui
#lang racket/gui
(play-sound "some-sound.wav" #f)
(play-sound "some-sound.wav" #f)
</syntaxhighlight>
</lang>


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
Load "guilib.ring"
Load "guilib.ring"
new qapp {
new qapp {
Line 736: Line 736:
func mute2
func mute2
q2.setmuted(true)
q2.setmuted(true)
</syntaxhighlight>
</lang>
Output:
Output:


Line 744: Line 744:
There aren't many mature sound libraries for Ruby. The {{libheader|RubyGems}} package [http://rubyforge.org/projects/win32utils/ win32-sound] can play WAV files on the Windows platform only.
There aren't many mature sound libraries for Ruby. The {{libheader|RubyGems}} package [http://rubyforge.org/projects/win32utils/ win32-sound] can play WAV files on the Windows platform only.


<lang ruby>require 'win32/sound'
<syntaxhighlight lang="ruby">require 'win32/sound'
include Win32
include Win32


Line 786: Line 786:


sleep 1
sleep 1
puts "the asynchronous sound is cancelled when the program exits"</lang>
puts "the asynchronous sound is cancelled when the program exits"</syntaxhighlight>


=={{header|Swift}}==
=={{header|Swift}}==
Uses AVFoundation's AVAudioPlayer.
Uses AVFoundation's AVAudioPlayer.
<lang Swift>import AVFoundation
<syntaxhighlight lang="swift">import AVFoundation


// This example uses AVAudioPlayer for playback.
// This example uses AVAudioPlayer for playback.
Line 867: Line 867:
player.player2.play()
player.player2.play()


CFRunLoopRun()</lang>
CFRunLoopRun()</syntaxhighlight>


=={{header|Tcl}}==
=={{header|Tcl}}==
{{libheader|snack}}
{{libheader|snack}}
<lang tcl>package require sound
<syntaxhighlight lang="tcl">package require sound
# Potentially also require driver support for particular formats
# Potentially also require driver support for particular formats


Line 890: Line 890:
after 30000 set done 1; vwait done
after 30000 set done 1; vwait done
s1 stop
s1 stop
s2 stop</lang>
s2 stop</syntaxhighlight>
Note that this library is capable of handling both short and long sounds (sound effects and music).
Note that this library is capable of handling both short and long sounds (sound effects and music).


Line 896: Line 896:


=={{header|TUSCRIPT}}==
=={{header|TUSCRIPT}}==
<lang tuscript>
<syntaxhighlight lang="tuscript">
$$ MODE TUSCRIPT
$$ MODE TUSCRIPT
audiofile="test.wav"
audiofile="test.wav"
ERROR/STOP OPEN (audiofile,READ,-std-)
ERROR/STOP OPEN (audiofile,READ,-std-)
BROWSE $audiofile
BROWSE $audiofile
</syntaxhighlight>
</lang>


=={{header|UNIX Shell}}==
=={{header|UNIX Shell}}==
<lang bash>#!/usr/bin/sh
<syntaxhighlight lang="bash">#!/usr/bin/sh


# play.sh
# play.sh
Line 911: Line 911:
# Usage: play.sh <recorded_sound.au>
# Usage: play.sh <recorded_sound.au>


cat $1 >> /dev/audio # Write file $1 to the speaker's Character Special (/dev/audio).</lang>
cat $1 >> /dev/audio # Write file $1 to the speaker's Character Special (/dev/audio).</syntaxhighlight>


=={{header|VBA}}==
=={{header|VBA}}==
Line 919: Line 919:
Volume can be set using the function waveOutSetVolume, see [http://support.microsoft.com/kb/118377/en-us http://support.microsoft.com/kb/118377/en-us].
Volume can be set using the function waveOutSetVolume, see [http://support.microsoft.com/kb/118377/en-us http://support.microsoft.com/kb/118377/en-us].


<syntaxhighlight lang="vb">
<lang vb>
Declare Function libPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" _
Declare Function libPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" _
(ByVal filename As String, ByVal Flags As Long) As Long
(ByVal filename As String, ByVal Flags As Long) As Long
Line 926: Line 926:
Call libPlaySound(sWav, 1) '1 to play asynchronously
Call libPlaySound(sWav, 1) '1 to play asynchronously
End Sub
End Sub
</syntaxhighlight>
</lang>
Type <pre>Playsound "d:\explode.wav"</pre> in the Immediate window and that sound will play. Nothing will happen if the file d:\explode.wav does not exist.
Type <pre>Playsound "d:\explode.wav"</pre> in the Immediate window and that sound will play. Nothing will happen if the file d:\explode.wav does not exist.


Line 934: Line 934:


It is certainly suitable for game sound effects (it's a game engine) and can play music at CD quality as well.
It is certainly suitable for game sound effects (it's a game engine) and can play music at CD quality as well.
<lang ecmascript>import "audio" for AudioEngine
<syntaxhighlight lang="ecmascript">import "audio" for AudioEngine
import "dome" for Process
import "dome" for Process


Line 966: Line 966:
}
}
}
}
}</lang>
}</syntaxhighlight>


=={{header|Z80 Assembly}}==
=={{header|Z80 Assembly}}==
Line 972: Line 972:
This routine streams an unsigned 8-bit PCM sample to the [[wp:Yamaha_YM2612|Yamaha 2612's]] [[wp:Digital-to-analog_converter|DAC]] in order to play a recorded sound. Unfortunately, the Sega Genesis's Z80 coprocessor can only access a limited pool of memory, so any PCM samples will have to be copied from the cartridge ROM to the shared memory area.
This routine streams an unsigned 8-bit PCM sample to the [[wp:Yamaha_YM2612|Yamaha 2612's]] [[wp:Digital-to-analog_converter|DAC]] in order to play a recorded sound. Unfortunately, the Sega Genesis's Z80 coprocessor can only access a limited pool of memory, so any PCM samples will have to be copied from the cartridge ROM to the shared memory area.


<lang z80>dac_enable equ &2B
<syntaxhighlight lang="z80">dac_enable equ &2B
dac_data equ &2A
dac_data equ &2A


Line 1,014: Line 1,014:
pop de
pop de
pop bc
pop bc
ret</lang>
ret</syntaxhighlight>


{{omit from|Applesoft BASIC}}
{{omit from|Applesoft BASIC}}