Record sound: Difference between revisions

Content added Content deleted
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
(Replaced code which no longer work on modern Linux with code using "arecord" and "aplay".)
Line 467: Line 467:


=={{header|Nim}}==
=={{header|Nim}}==
{{trans|C}}
{{trans|Go}}
This code is for Linux systems and uses “arecord” and “aplay”. Previous code which used “/dev/dsp” no longer works on modern OS.
<lang nim>proc record(bytes): auto =
<lang nim>import osproc, strutils
var f = open("/dev/dsp")
result = newSeq[int8](bytes)
discard f.readBytes(result, 0, bytes)


var name = ""
proc play(buf) =
while name.len == 0:
var f = open("/dev/dsp", fmWrite)
stdout.write "Enter output file name (without extension): "
f.write(buf)
name = stdin.readLine().strip()
f.close
name.add ".wav"


var p = record(65536)
var rate = 0
while rate notin 2000..19_200:
play(p)</lang>
stdout.write "Enter sampling rate in Hz (2000 to 192000): "
try: rate = parseInt(stdin.readLine().strip())
except ValueError: discard

var duration = 0
while duration notin 5..30:
stdout.write "Enter duration in seconds (5 to 30): "
try: duration = parseInt(stdin.readLine().strip())
except ValueError: discard

echo "OK, start speaking now..."
# Default arguments: -c 1, -t wav. Note that only signed 16 bit format supported.
let args = ["-r", $rate, "-f", "S16_LE", "-d", $duration, name]
echo execProcess("arecord", args = args, options = {poStdErrToStdOut, poUsePath})

echo "'$1' created on disk and will now be played back..." % name
echo execProcess("aplay", args = [name], options = {poStdErrToStdOut, poUsePath})
echo "Playback completed"</lang>


=={{header|OCaml}}==
=={{header|OCaml}}==