Record sound: Difference between revisions

40,324 bytes added ,  1 month ago
Initial submission
No edit summary
(Initial submission)
 
(36 intermediate revisions by 18 users not shown)
Line 1:
{{task|Sound}} [[Category:Temporal media]]
{{task}}
{{omit from|AWK}}
{{omit from|Brlcad}}
{{omit from|HTML}}
{{omit from|LFE}}
{{omit from|Logtalk}}
{{omit from|Lotus 123 Macro Scripting}}
{{omit from|Maxima}}
{{omit from|ML/I}}
{{omit from|Openscad}}
{{omit from|PARI/GP}}
{{omit from|TI-83 BASIC}}
{{omit from|TPP}}
{{omit from|zkl}}
 
Record a monophonic 16-bit PCM sound into either memory space, a file or array.
 
(This task neglects to specify the sample rate, and whether to use signed samples. The programs in this page might use signed 16-bit or unsigned 16-bit samples, at 8000 Hz, 44100 Hz, or any other sample rate. Therefore, these programs might not record sound in the same format.)
The programs in this page might use signed 16-bit or unsigned 16-bit samples, at 8000 Hz, 44100 Hz, or any other sample rate.
Therefore, these programs might not record sound in the same format.)
 
 
=={{header|Ada}}==
{{libheader|ASFML}}
<syntaxhighlight lang="ada">
with Sf.Audio.SoundBufferRecorder;
with Sf.Audio.SoundBuffer;
with Ada.Text_IO;
 
procedure Record_Sound is
use Sf, Sf.Audio, Ada.Text_IO;
Sound_Buffer_Recorder : constant sfSoundBufferRecorder_Ptr := SoundBufferRecorder.create;
Sound_Buffer : sfSoundBuffer_Ptr;
begin
 
if Sound_Buffer_Recorder = null then
Put_Line (Standard_Error, "Error: sound recorder not available!");
return;
end if;
 
-- By default the recording is in 16-bit mono. Using the
-- setChannelCount method you can change the number of channels
-- used by the audio capture device to record.
if SoundBufferRecorder.start (Sound_Buffer_Recorder, sampleRate => 44_100) /= sfTrue then
Put_Line (Standard_Error, "Error: sound recorder cannot start!");
return;
end if;
 
delay 10.0;
SoundBufferRecorder.stop (Sound_Buffer_Recorder);
 
Sound_Buffer := SoundBufferRecorder.getBuffer (Sound_Buffer_Recorder);
if SoundBuffer.saveToFile (Sound_Buffer, filename => "output.ogg") /= sfTrue then
Put_Line (Standard_Error, "Error: recorded sound could not be saved!");
end if;
 
SoundBufferRecorder.destroy (Sound_Buffer_Recorder);
end Record_Sound;
</syntaxhighlight>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight lang="autohotkey">name := "sample"
waitsec := 5
Tooltip Recording %name%.wav
Line 41 ⟶ 95:
; For more intuitive functions, see the MCI library by jballi.
; doc: http://www.autohotkey.net/~jballi/MCI/v1.1/MCI.html
; download: http://www.autohotkey.net/~jballi/MCI/v1.1/MCI.ahk</langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> wavfile$ = @dir$ + "capture.wav"
bitspersample% = 16
channels% = 2
Line 80 ⟶ 134:
SYS "mciSendString", "close capture", 0, 0, 0
PRINT "Captured audio is stored in " wavfile$</langsyntaxhighlight>
 
{{omit from|Batch File}}
Line 88 ⟶ 142:
Read/write raw device <code>/dev/dsp</code>. On Linux you need access to said device, meaning probably you should be in audio user group.
 
<langsyntaxhighlight lang="c">#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
Line 117 ⟶ 171:
play(p, 65536);
return 0;
}</langsyntaxhighlight>
 
=={{header|C++}}==
Uses Windows MCI
<langsyntaxhighlight lang="cpp">
#include <iostream>
#include <string>
Line 215 ⟶ 269:
return 0;
}
</syntaxhighlight>
</lang>
 
=={{header|ChucK}}==
 
<langsyntaxhighlight lang="c">// chuck this with other shreds to record to file
// example> chuck foo.ck bar.ck rec
 
Line 241 ⟶ 295:
// infinite time loop...
// ctrl-c will stop it, or modify to desired duration
while( true ) 1::second => now;</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
{{trans|C}}
<syntaxhighlight lang="lisp">
(defun record (n)
(with-open-file (in "/dev/dsp" :element-type '(unsigned-byte 8))
(loop repeat n collect (read-byte in))
)
)
(defun play (byte-list)
(with-open-file (out "/dev/dsp" :direction :output :element-type '(unsigned-byte 8) :if-exists :append)
(mapcar (lambda (b) (write-byte b out)) byte-list)
)
)
(play (record 65536))
</syntaxhighlight>
 
=={{header|Diego}}==
This <code>funct</code>ion returns a <code>{wav}</code> variable recorded from a thing in the mist. It understands that the found <code>thing</code> has a microphone, so will have microphone knowledge. If the caller does not have microphone knowledge, the callee will train the caller on first request.
<syntaxhighlight lang="diego">begin_funct({wav}, Record sound);
set_decision(linger);
find_thing()_first()_microphone()_bitrate(16)_tech(PCM)_samplerate(signed16, unsigned16)_rangefrom(8000, Hz)_rangeto(44100, Hz)_export(.wav)
? with_found()_microphone()_label(mic);
: err_funct[]_err(Sorry, no one has a microphone!);
exit_funct[];
;
with_microphone[mic]_record()_durat({secs}, 30)_var(recording);
[Record sound]_ret([recording]);
reset_decision();
end_funct[];
 
// Record a monophonic 16-bit PCM sound into memory space:
exec_funct(Record sound)_var(PCMRecording)_me(); // The variable 'PCMRecording' is the sound in memory space
 
// Record a monophonic 16-bit PCM sound into a file or array:
exec_funct(Record sound)_file(foo.wav)_me(); // The file 'foo.wav' is the sound in a file</syntaxhighlight>
 
This is the <code>instruct</code>ion version, where the thing keeps the recording.
<syntaxhighlight lang="diego">begin_instruct(Record sound);
set_decision(linger);
find_thing()_first()_microphone()_bitrate(16)_tech(PCM)_samplerate(signed16, unsigned16)_rangefrom(8000, Hz)_rangeto(44100, Hz)_export(.wav)
? with_found()_label(recorder)_microphone()_label(mic);
: err_instruct[]_err(Sorry, no one has a microphone!);
exit_instruct[];
;
with_microphone[mic]_record()_durat({secs}, 30)_var(recording);
reset_decision();
end_instruct[];
 
// Record a monophonic 16-bit PCM sound into memory space:
exec_instruct(Record sound)_me();
with_thing[recorder]_microphone[mic]_var[recording]_var(PCMRecording); // The variable 'PCMRecording' is the sound in memory space
 
// Record a monophonic 16-bit PCM sound into a file or array:
exec_instruct(Record sound)_me();
with_thing[recorder]_microphone[mic]_var[recording]_file(foo.wav)_me(); // The file 'foo.wav' is the sound in a file</syntaxhighlight>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="vbnet">#define PI 4 * Atn(1)
 
' Constants for the audio format
Dim Shared As Integer SAMPLE_RATE = 44100
Dim Shared As Integer BITS_PER_SAMPLE = 16
Dim Shared As Integer NUM_CHANNELS = 1
 
' Generates a sine wave
Sub generateSineWave(buffer() As Short, frequency As Double)
Dim As Double increment = 2.0 * PI * frequency / SAMPLE_RATE
Dim As Double x = 0.0
For i As Integer = 0 To Ubound(buffer)
buffer(i) = (Sin(x) * 32767.0)
x += increment
Next i
End Sub
 
' Write the header of the .wav file
Sub writeWaveHeader(file As Integer, numSamples As Integer)
' Write the RIFF header
Print #file, "RIFF";
Put #file, , numSamples * 2 + 36 ' File size
Print #file, "WAVE";
Dim As Integer SB = 16, FA = 1
' Write the fmt sub-block
Print #file, "fmt ";
Put #file, , SB ' Size of the fmt sub-block
Put #file, , FA ' Audio format (1 = PCM)
Put #file, , NUM_CHANNELS ' Number of channels
Put #file, , SAMPLE_RATE ' Sample rate
Put #file, , SAMPLE_RATE * NUM_CHANNELS * BITS_PER_SAMPLE / 8 ' Byte rate
Put #file, , NUM_CHANNELS * BITS_PER_SAMPLE / 8 ' Alineación de bloques
Put #file, , BITS_PER_SAMPLE ' Bits per sample
' Write the data sub-block
Print #file, "data";
Put #file, , numSamples * 2 ' Data size
End Sub
 
Dim As Integer file = Freefile
Open "output.wav" For Binary As #file
 
' Generates a 440 Hz sine wave for 5 seconds
Dim As Integer numSamples = SAMPLE_RATE * 5
Dim As Short buffer(numSamples - 1)
generateSineWave(buffer(), 440.0)
 
' Write the .wav file
writeWaveHeader(file, numSamples)
Put #file, , buffer()
 
Close #file</syntaxhighlight>
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
// ---------------
include "NSLog.incl"
include "Tlbx Speech.incl"
include "Tlbx AVFoundation.incl"
 
output "RecordToTextDemo"
 
#plist CFBundleIdentifier @"com.futurebasic.RecordToTextDemo"
#plist NSSpeechRecognitionUsageDescription @"Enable speech recognition."
#plist NSMicrophoneUsageDescription @"This app requires microphone access."
 
#define FILE_URL fn URLByAppendingPathComponent(fn FileManagerURLForApplicationDirectory,@"MyAudioFile.m4a")
#define RECORDER_KEY @"recorder"
 
 
_window = 1
begin enum 1
_recordBtn
end enum
 
 
void local fn FixViews
dispatchmain
AVAudioRecorderRef recorder = fn AppProperty( RECORDER_KEY )
if ( recorder )
button _recordBtn,,, @"Stop Recording",,,, _window
else
button _recordBtn,,, @"Start Recording",,,, _window
end if
dispatchend
end fn
 
 
void local fn BuildWindow
window _window, @"Record Demo", (0,0,480,270)
button _recordBtn,,, @"Start Recording", (13,13,129,32)
end fn
 
 
void local fn MyRecognitionTaskHandler( ref as SFSpeechRecognizerRef, result as SFSpeechRecognitionResultRef, err as ErrorRef, userData as ptr )
SFTranscriptionRef transcription = fn SFSpeechRecognitionResultBestTranscription( result )
if ( err )
NSLog(@"error")
else
if ( fn SFSpeechRecognitionResultIsFinal( result ) )
NSLog(@"%@",fn SFTranscriptionFormattedString( transcription ))
end if
end if
end fn
 
 
void local fn MyRequestAuthorization( status as SFSpeechRecognizerAuthorizationStatus, userData as ptr )
SFSpeechRecognizerRef recognizer
SFSpeechURLRecognitionRequestRef request
select ( status )
case SFSpeechRecognizerAuthorizationStatusNotDetermined : NSLog(@"Authorization not determined")
case SFSpeechRecognizerAuthorizationStatusDenied : NSLog(@"Authorization denied")
case SFSpeechRecognizerAuthorizationStatusRestricted : NSLog(@"Authorization restricted")
case SFSpeechRecognizerAuthorizationStatusAuthorized
recognizer = fn SFSpeechRecognizerInit
if ( fn SFSpeechRecognizerIsAvailable( recognizer ) )
if ( FILE_URL )
fn SoundPlay( fn SoundWithContentsOfURL( FILE_URL, YES ) )
request = fn SFSpeechURLRecognitionRequestWithURL( FILE_URL )
SFSpeechRecognizerSetSupportsOnDeviceRecognition( recognizer, YES )
fn SFSpeechRecognizerRecognitionTaskWithResultHandler( recognizer, request, @fn MyRecognitionTaskHandler, NULL )
end if
else
NSLog(@"Speech recognizer not available")
end if
end select
end fn
 
 
void local fn RecognizeSpeech
SFSpeechRecognizerRequestAuthorization( @fn MyRequestAuthorization, NULL )
end fn
 
 
void local fn RecordAudio
CFDictionaryRef settings = @{
AVFormatIDKey:@(kAudioFormatMPEG4AAC),
AVEncoderAudioQualityKey:@(AVAudioQualityHigh),
AVSampleRateKey:@44100.0,
AVNumberOfChannelsKey:@1,
AVLinearPCMBitDepthKey:@16}
AVAudioFormatRef format = fn AVAudioFormatWithSettings( settings )
AVAudioRecorderRef recorder = fn AVAudioRecorderWithFormat( FILE_URL, format, NULL )
AppSetProperty( RECORDER_KEY, recorder )
fn FixViews
fn AVAudioRecorderRecord( recorder )
end fn
 
 
void local fn MyRequestAccessHandler( granted as BOOL, userData as ptr )
if ( granted ) then fn RecordAudio
end fn
 
 
void local fn StartStopRecording
AVAudioRecorderRef recorder = fn AppProperty( RECORDER_KEY )
AVAuthorizationStatus status
if ( recorder )
AVAudioRecorderStop( recorder )
AppRemoveProperty( RECORDER_KEY )
fn FixViews
fn RecognizeSpeech
else
status = fn AVCaptureDeviceAuthorizationStatus( AVMediaTypeAudio )
select ( status )
case AVAuthorizationStatusDenied : NSLog(@"Denied")
case AVAuthorizationStatusRestricted : NSLog(@"Restricted")
case AVAuthorizationStatusAuthorized : fn RecordAudio
case AVAuthorizationStatusNotDetermined
AVCaptureDeviceRequestAccess( AVMediaTypeAudio, @fn MyRequestAccessHandler, NULL )
end select
end if
end fn
 
 
void local fn DoDialog( ev as long, tag as long )
select ( ev )
case _btnClick
select ( tag )
case _recordBtn : fn StartStopRecording
end select
end select
end fn
 
fn BuildWindow
 
on dialog fn DoDialog
 
HandleEvents
</syntaxhighlight>
 
 
=={{header|Go}}==
{{works with|Ubuntu 16.04}}
<br>
As Go does not have any audio support in its standard library, this invokes the 'arecord' command-line utility to record sound from the internal microphone (assuming there's no other audio input device present) and saves it to a monophonic, signed 16-bit .wav file. It then plays it back using the 'aplay' utility.
 
The file name, sampling rate and duration can all be set by the user.
<syntaxhighlight lang="go">package main
 
import (
"bufio"
"fmt"
"log"
"os"
"os/exec"
"strconv"
)
 
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
 
func main() {
scanner := bufio.NewScanner(os.Stdin)
name := ""
for name == "" {
fmt.Print("Enter output file name (without extension) : ")
scanner.Scan()
name = scanner.Text()
check(scanner.Err())
}
name += ".wav"
 
rate := 0
for rate < 2000 || rate > 192000 {
fmt.Print("Enter sampling rate in Hz (2000 to 192000) : ")
scanner.Scan()
input := scanner.Text()
check(scanner.Err())
rate, _ = strconv.Atoi(input)
}
rateS := strconv.Itoa(rate)
 
dur := 0.0
for dur < 5 || dur > 30 {
fmt.Print("Enter duration in seconds (5 to 30) : ")
scanner.Scan()
input := scanner.Text()
check(scanner.Err())
dur, _ = strconv.ParseFloat(input, 64)
}
durS := strconv.FormatFloat(dur, 'f', -1, 64)
 
fmt.Println("OK, start speaking now...")
// Default arguments: -c 1, -t wav. Note only signed 16 bit format supported.
args := []string{"-r", rateS, "-f", "S16_LE", "-d", durS, name}
cmd := exec.Command("arecord", args...)
err := cmd.Run()
check(err)
 
fmt.Printf("'%s' created on disk and will now be played back...\n", name)
cmd = exec.Command("aplay", name)
err = cmd.Run()
check(err)
fmt.Println("Play-back completed.")
}</syntaxhighlight>
 
=={{header|GUISS}}==
Line 248 ⟶ 624:
Here we activate the Microsoft Windows '95 Sound Recorder:
 
<langsyntaxhighlight lang="guiss">Start,Programs,Accessories,Sound Recorder,Button:Record</langsyntaxhighlight>
 
=={{header|Java}}==
Java can record sound without external libraries.
<syntaxhighlight lang="java">
import java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.io.File;
 
import javax.sound.sampled.AudioFileFormat.*;
 
public final class SoundRecorder {
 
public static void main(String[] args) {
SoundRecorder recorder = new SoundRecorder();
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.schedule( () -> recorder.finish(), 10, TimeUnit.SECONDS);
scheduler.shutdown();
 
recorder.start();
}
private void start() {
try {
AudioFormat format = createAudioFormat();
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
if ( ! AudioSystem.isLineSupported(info) ) {
System.out.println("Data line format is not supported");
Runtime.getRuntime().exit(0);
}
line = (TargetDataLine) AudioSystem.getLine(info);
line.open(format);
line.start();
System.out.println("Starting to capture and record audio");
AudioInputStream audioInputStream = new AudioInputStream(line);
AudioSystem.write(audioInputStream, audioFileType, wavFile);
} catch (LineUnavailableException | IOException exception) {
exception.printStackTrace(System.err);
}
}
 
private AudioFormat createAudioFormat() {
final float sampleRate = 16_000.0F;
final int sampleSizeInBits = 16;
final int channels = 1;
final boolean signed = true;
final boolean bigEndian = true;
// Monophonic 16-bit PCM audio format
return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);
}
 
private void finish() {
line.stop();
line.close();
System.out.println("Finished capturing and recording audio");
}
private TargetDataLine line;
private final File wavFile = new File("SoundRecorder.wav");
private final AudioFileFormat.Type audioFileType = AudioFileFormat.Type.WAVE;
}</syntaxhighlight>
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">
using PortAudio, LibSndFile
 
stream = PortAudioStream("Microphone (USB Microphone)", 1, 0) # 44100 samples/sec
buf = read(stream, 441000)
save("recorded10sec.wav", buf)
</syntaxhighlight>
 
=={{header|Kotlin}}==
{{trans|Scala}}
<syntaxhighlight lang="scala">// version 1.1.3
 
import java.io.File
import javax.sound.sampled.*
 
const val RECORD_TIME = 20000L // twenty seconds say
 
fun main(args: Array<String>) {
val wavFile = File("RecordAudio.wav")
val fileType = AudioFileFormat.Type.WAVE
val format = AudioFormat(16000.0f, 16, 2, true, true)
val info = DataLine.Info(TargetDataLine::class.java, format)
val line = AudioSystem.getLine(info) as TargetDataLine
 
// Creates a new thread that waits for 'RECORD_TIME' before stopping
Thread(object: Runnable {
override fun run() {
try {
Thread.sleep(RECORD_TIME)
}
catch (ie: InterruptedException) {
println(ie.message)
}
finally {
line.stop()
line.close()
}
println("Finished")
}
}).start()
 
// Captures the sound and saves it in a WAV file
try {
if (AudioSystem.isLineSupported(info)) {
line.open(format)
line.start()
println("Recording started")
AudioSystem.write(AudioInputStream(line), fileType, wavFile)
}
else println("Line not supported")
}
catch (lue: LineUnavailableException) {
println(lue.message)
}
}</syntaxhighlight>
 
=={{header|Liberty BASIC}}==
LB can easily send a MCI string to the OS, or extra routines eg SOX, so a minimal solution could be
<syntaxhighlight lang="lb">
<lang lb>
run "sndrec32.exe"
</syntaxhighlight>
</lang>
A more direct way is..
<syntaxhighlight lang="lb">
<lang lb>
print "Starting 5 sec. recording..."
r$ =mciSendString$( "open new type waveaudio alias capture")
Line 292 ⟶ 793:
end if
end function
</syntaxhighlight>
</lang>
 
=={{header|MathematicaLiveCode}}==
This example sets some aspects of a sound recording individually and also shows use of the sound input dialog where a user can conveniently set them as well, either may be used.<syntaxhighlight lang="livecode">command makeRecording
<lang Mathematica>SystemDialogInput["RecordSound"]</lang>
set the dontUseQT to false -- on windows use true
set the recordFormat to "wave" -- can be wav,aiff, au
set the recordRate to 44.1 -- sample at 44100 Hz
set the recordSampleSize to 16 --default is 8 bit
ask file "Save recording as"
if it is not empty then
answer record --show sound input dialog with presets above
record sound file it -- actual record command
wait 10 seconds
stop recording
end if
end makeRecording</syntaxhighlight>
 
=={{header|NimrodMathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">SystemDialogInput["RecordSound"]</syntaxhighlight>
{{trans|C}}
<lang nimrod>proc record(bytes): auto =
var f = open("/dev/dsp")
result = newSeq[int8](bytes)
discard f.readBytes(result, 0, bytes)
 
=={{header|Nim}}==
proc play(buf) =
{{trans|Go}}
var f = open("/dev/dsp", fmWrite)
This code is for Linux systems and uses “arecord” and “aplay”. Previous code which used “/dev/dsp” no longer works on modern OS.
f.write(buf)
This code is a direct translation from Go version, but uses integers instead of floats for duration.
f.close
<syntaxhighlight lang="nim">import osproc, strutils
 
var pname = record(65536)""
while name.len == 0:
play(p)</lang>
stdout.write "Enter output file name (without extension): "
name = stdin.readLine().strip()
name.add ".wav"
 
var rate = 0
while rate notin 2000..19_200:
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 is 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"</syntaxhighlight>
 
=={{header|OCaml}}==
{{trans|C}}
 
<langsyntaxhighlight lang="ocaml">#load "unix.cma"
let record bytes =
Line 335 ⟶ 869:
let bytes = 65536 in
let p = record bytes in
play p bytes</langsyntaxhighlight>
 
=={{header|Phix}}==
{{trans|BBC_BASIC}}
{{trans|C}}
{{trans|Go}}
<!--<syntaxhighlight lang="phix">(notonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Record_sound.exw
-- =============================
--</span>
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (file i/o)</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">wavfile</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"capture.wav"</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">bitspersample</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">16</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">channels</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">samplespersec</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">44100</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">alignment</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">bitspersample</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">channels</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">8</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">bytespersec</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">alignment</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">samplespersec</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">params</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">" bitspersample %d channels %d alignment %d samplespersec %d bytespersec %d"</span><span style="color: #0000FF;">,</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">bitspersample</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">channels</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">alignment</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">samplespersec</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">bytespersec</span><span style="color: #0000FF;">}),</span>
<span style="color: #000000;">error_size</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2048</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">winmm</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">NULL</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">xmciSendString</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">pError</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">mciSendString</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">msg</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">winmm</span><span style="color: #0000FF;">=</span><span style="color: #004600;">NULL</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">winmm</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">open_dll</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"winmm.dll"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">xmciSendString</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">define_c_func</span><span style="color: #0000FF;">(</span><span style="color: #000000;">winmm</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"mciSendStringA"</span><span style="color: #0000FF;">,</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">C_PTR</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- LPCTSTR lpszCommand</span>
<span style="color: #000000;">C_PTR</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- LPTSTR lpszReturnString</span>
<span style="color: #000000;">C_INT</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- UINT cchReturn</span>
<span style="color: #000000;">C_PTR</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- HANDLE hwndCallback</span>
<span style="color: #000000;">C_INT</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- MCIERROR</span>
<span style="color: #000000;">pError</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">allocate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">error_size</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">c_func</span><span style="color: #0000FF;">(</span><span style="color: #000000;">xmciSendString</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">msg</span><span style="color: #0000FF;">,</span><span style="color: #000000;">pError</span><span style="color: #0000FF;">,</span><span style="color: #000000;">error_size</span><span style="color: #0000FF;">,</span><span style="color: #004600;">NULL</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">crash</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"error %0x: %s"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">peek_string</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pError</span><span style="color: #0000FF;">)})</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">get</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> <span style="color: #000080;font-style:italic;">-- get_bytes()</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">record</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">bytes</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">fn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">open</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"/dev/dsp"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"rb"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">fn</span><span style="color: #0000FF;">=-</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #008000;">""</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">get_bytes</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">bytes</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">close</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">play</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">buf</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">buf</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">fn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">open</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"/dev/dsp"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"wb"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">fn</span><span style="color: #0000FF;">!=-</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">buf</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">close</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">WINDOWS</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">mciSendString</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"close all"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">mciSendString</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"open new type waveaudio alias capture"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">mciSendString</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"set capture"</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">params</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Press SPACE to start recording..."</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">while</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()!=</span><span style="color: #008000;">' '</span> <span style="color: #008080;">do</span> <span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #000000;">mciSendString</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"record capture"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Recording, press SPACE to stop..."</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">while</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()!=</span><span style="color: #008000;">' '</span> <span style="color: #008080;">do</span> <span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #000000;">mciSendString</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"stop capture"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">mciSendString</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"save capture "</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">wavfile</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">mciSendString</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"delete capture"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">mciSendString</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"close capture"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Captured audio is stored in "</span><span style="color: #0000FF;">&</span><span style="color: #000000;">wavfile</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">elsif</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">LINUX</span> <span style="color: #008080;">then</span>
<span style="color: #000080;font-style:italic;">-- warning: untested</span>
<span style="color: #000000;">play</span><span style="color: #0000FF;">(</span><span style="color: #000000;">record</span><span style="color: #0000FF;">(</span><span style="color: #000000;">65536</span><span style="color: #0000FF;">))</span>
<span style="color: #000080;font-style:italic;">-- -- alternative, from Go (ditto)
-- string name = "test.wav",
-- rate = "2000", -- (2000..192000 Hz)
-- durn = "5" -- (5 to 30 seconds)
-- printf(1,"OK, start speaking now...\n")
-- -- Default arguments: -c 1, -t wav. Note only signed 16 bit format supported.
-- string cmd = sprintf("arecord -r %s -f S16_LE -d %s %s", {rate,durn,name})
-- {} = system_exec(cmd)
-- printf(1,"'%s' created on disk and will now be played back...\n", {name})
-- {} = system_exec("aplay "&name)
-- printf(1,"Play-back completed.\n")</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</syntaxhighlight>-->
 
=={{header|PicoLisp}}==
 
<langsyntaxhighlight PicoLisplang="picolisp">(in '(rec -q -c1 -tu16 - trim 0 2) # Record 2 seconds
(make
(while (rd 2)
(link @) ) ) )</langsyntaxhighlight>
 
Output:
Line 349 ⟶ 974:
=={{header|Python}}==
 
<langsyntaxhighlight lang="python">import pyaudio
 
chunk = 1024
Line 365 ⟶ 990:
 
data = stream.read(chunk)
print [ord(i) for i in data]</langsyntaxhighlight>
 
=={{header|Racket}}==
{{trans|C}}
<langsyntaxhighlight lang="racket">
#lang racket
(define (record n) (with-input-from-file "/dev/dsp" ( () (read-bytes n))))
(define (play bs) (display-to-file bs "/dev/dsp" #:exists 'append))
(play (record 65536))
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
Slightly modified from an example provided with the <code>Audio::PortAudio</code> module distribution.
<syntaxhighlight lang="raku" line>use Audio::PortAudio;
use Audio::Sndfile;
 
sub MAIN(Str $filename, Str :$source, Int :$buffer = 256) {
my $pa = Audio::PortAudio.new;
my $format = Audio::Sndfile::Info::Format::WAV +| Audio::Sndfile::Info::Subformat::PCM_16;
my $out-file = Audio::Sndfile.new(:$filename, channels => 1, samplerate => 44100, :$format, :w);
my $st;
if $source.defined {
my $index = 0;
for $pa.devices -> $device {
if $device.name eq $source {
else {
my $la = $device.default-high-input-latency;
my $si = Audio::PortAudio::StreamParameters.new(device => $index,
channel-count => 1,
sample-format => Audio::PortAudio::StreamFormat::Float32,
suggested-latency => ($la || 0.05e0 ));
$st = $pa.open-stream($si, Audio::PortAudio::StreamParameters, 44100, $buffer );
last;
}
 
}
$index++;
}
die "Couldn't find a device for '$source'" if !$st.defined;
}
else {
$st = $pa.open-default-stream(2,0, Audio::PortAudio::StreamFormat::Float32, 44100, $buffer);
}
$st.start;
my $p = Promise.new;
signal(SIGINT).act({
say "stopping recording";
$p.keep: "stopped";
$out-file.close;
$st.close;
exit;
});
my Channel $write-channel = Channel.new;
my $write-promise = start {
react {
whenever $write-channel -> $item {
if $p.status ~~ Planned {
$out-file.write-float($item[0], $item[1]);
$out-file.sync;
}
else {
done;
}
}
}
};
 
loop {
if $p.status ~~ Planned {
my $f = $buffer || $st.read-available;
if $f > 0 {
my $buff = $st.read($f,2, num32);
$write-channel.send([$buff, $f]);
}
}
else {
last;
}
}
 
}</syntaxhighlight>
 
=={{header|Scala}}==
{{libheader|Scala}}
[[Category:Scala Implementations]]{{libheader|Scala}}<lang Scala>import java.io.{File, IOException}
<syntaxhighlight lang="scala">import java.io.{File, IOException}
import javax.sound.sampled.{AudioFileFormat, AudioFormat, AudioInputStream}
import javax.sound.sampled.{AudioSystem, DataLine, LineUnavailableException, TargetDataLine}
Line 427 ⟶ 1,126:
case ioe: IOException => ioe.printStackTrace()
}
}</langsyntaxhighlight>
 
=={{header|Tcl}}==
{{libheader|Snack}}<langsyntaxhighlight lang="tcl">package require sound
 
# Helper to do a responsive wait
Line 448 ⟶ 1,147:
 
# Destroy the recording object
$recording destroy</langsyntaxhighlight>
 
=={{header|Wee Basic}}==
{{omit from|AWK}}
<syntaxhighlight lang="wee basic">print 1 "Recording..."
{{omit from|Brlcad}}
micrec
{{omit from|HTML}}
print 1 "Playing..."
{{omit from|Logtalk}}
micpla
{{omit from|Lotus 123 Macro Scripting}}
end</syntaxhighlight>
{{omit from|ML/I}}
{{omit from|Maxima}}
{{omit from|Openscad}}
{{omit from|PARI/GP}}
{{omit from|TI-83 BASIC}}
{{omit from|TPP}}
{{omit from|zkl}}
 
=={{header|Wren}}==
[[Category:Temporal media]]
{{trans|Go}}
The ability to call external processes such as ''arecord'' is expected to be added to Wren-cli in the next release. In the meantime, we embed the following Wren script in a C host to complete this task.
<syntaxhighlight lang="wren">/* Record_sound.wren */
 
class C {
foreign static getInput(maxSize)
 
foreign static arecord(args)
 
foreign static aplay(name)
}
 
var name = ""
while (name == "") {
System.write("Enter output file name (without extension) : ")
name = C.getInput(80)
}
name = name + ".wav"
 
var rate = 0
while (!rate || !rate.isInteger || rate < 2000 || rate > 192000) {
System.write("Enter sampling rate in Hz (2000 to 192000) : ")
rate = Num.fromString(C.getInput(6))
}
var rateS = rate.toString
 
var dur = 0
while (!dur || dur < 5 || dur > 30) {
System.write("Enter duration in seconds (5 to 30) : ")
dur = Num.fromString(C.getInput(5))
}
var durS = dur.toString
 
System.print("\nOK, start speaking now...")
// Default arguments: -c 1, -t wav. Note only signed 16 bit format supported.
var args = ["-r", rateS, "-f", "S16_LE", "-d", durS, name]
C.arecord(args.join(" "))
 
System.print("\n'%(name)' created on disk and will now be played back...")
C.aplay(name)
System.print("\nPlay-back completed.")</syntaxhighlight>
<br>
We now embed this in the following C program, compile and run it.
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include <string.h>
#include "wren.h"
 
void C_getInput(WrenVM* vm) {
int maxSize = (int)wrenGetSlotDouble(vm, 1) + 2;
char input[maxSize];
fgets(input, maxSize, stdin);
__fpurge(stdin);
input[strcspn(input, "\n")] = 0;
wrenSetSlotString(vm, 0, (const char*)input);
}
 
void C_arecord(WrenVM* vm) {
const char *args = wrenGetSlotString(vm, 1);
char command[strlen(args) + 8];
strcpy(command, "arecord ");
strcat(command, args);
system(command);
}
 
void C_aplay(WrenVM* vm) {
const char *name = wrenGetSlotString(vm, 1);
char command[strlen(name) + 6];
strcpy(command, "aplay ");
strcat(command, name);
system(command);
}
 
WrenForeignMethodFn bindForeignMethod(
WrenVM* vm,
const char* module,
const char* className,
bool isStatic,
const char* signature) {
if (strcmp(module, "main") == 0) {
if (strcmp(className, "C") == 0) {
if (isStatic && strcmp(signature, "getInput(_)") == 0) return C_getInput;
if (isStatic && strcmp(signature, "arecord(_)") == 0) return C_arecord;
if (isStatic && strcmp(signature, "aplay(_)") == 0) return C_aplay;
}
}
return NULL;
}
 
static void writeFn(WrenVM* vm, const char* text) {
printf("%s", text);
}
 
void errorFn(WrenVM* vm, WrenErrorType errorType, const char* module, const int line, const char* msg) {
switch (errorType) {
case WREN_ERROR_COMPILE:
printf("[%s line %d] [Error] %s\n", module, line, msg);
break;
case WREN_ERROR_STACK_TRACE:
printf("[%s line %d] in %s\n", module, line, msg);
break;
case WREN_ERROR_RUNTIME:
printf("[Runtime Error] %s\n", msg);
break;
}
}
 
char *readFile(const char *fileName) {
FILE *f = fopen(fileName, "r");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
rewind(f);
char *script = malloc(fsize + 1);
fread(script, 1, fsize, f);
fclose(f);
script[fsize] = 0;
return script;
}
 
int main(int argc, char **argv) {
WrenConfiguration config;
wrenInitConfiguration(&config);
config.writeFn = &writeFn;
config.errorFn = &errorFn;
config.bindForeignMethodFn = &bindForeignMethod;
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "Record_sound.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
switch (result) {
case WREN_RESULT_COMPILE_ERROR:
printf("Compile Error!\n");
break;
case WREN_RESULT_RUNTIME_ERROR:
printf("Runtime Error!\n");
break;
case WREN_RESULT_SUCCESS:
break;
}
wrenFreeVM(vm);
free(script);
return 0;
}</syntaxhighlight>
729

edits