Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
5
Task/Record-sound/00-META.yaml
Normal file
5
Task/Record-sound/00-META.yaml
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
category:
|
||||
- Temporal media
|
||||
from: http://rosettacode.org/wiki/Record_sound
|
||||
note: Sound
|
||||
6
Task/Record-sound/00-TASK.txt
Normal file
6
Task/Record-sound/00-TASK.txt
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
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.)
|
||||
|
||||
36
Task/Record-sound/AutoHotkey/record-sound.ahk
Normal file
36
Task/Record-sound/AutoHotkey/record-sound.ahk
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
name := "sample"
|
||||
waitsec := 5
|
||||
Tooltip Recording %name%.wav
|
||||
MCI_SendString("close all wait")
|
||||
MCI_SendString("open new type waveaudio alias " . name)
|
||||
MCI_SendString("set " . name . " time format ms wait")
|
||||
;MCI_SendString("set " . name . " bitspersample 16 wait")
|
||||
;MCI_SendString("set " . name . " channels 1 wait")
|
||||
;MCI_SendString("set " . name . " samplespersec 16000 wait")
|
||||
;MCI_SendString("set " . name . " alignment 1 wait")
|
||||
;MCI_SendString("set " . name . " bytespersec 8000 wait")
|
||||
MCI_SendString("record " . name)
|
||||
Sleep waitsec*1000
|
||||
MCI_SendString("stop " . name . " wait")
|
||||
MCI_SendString("save " . name . " """ . name . ".wav""")
|
||||
Tooltip Finished ... Playing
|
||||
MCI_SendString("delete " . name)
|
||||
MCI_SendString("close " . name . " wait")
|
||||
MCI_SendString("open """ . name . ".wav"" type waveaudio alias " . name)
|
||||
MCI_SendString("play " . name . " wait")
|
||||
MCI_SendString("close " . name . " wait")
|
||||
Tooltip
|
||||
Return
|
||||
|
||||
MCI_SendString(p_lpszCommand,ByRef r_lpszReturnString="",p_hwndCallback=0) {
|
||||
VarSetCapacity(r_lpszReturnString,512,0)
|
||||
Return DllCall("winmm.dll\mciSendString" . (A_IsUnicode ? "W":"A")
|
||||
,"Str",p_lpszCommand ;-- lpszCommand
|
||||
,"Str",r_lpszReturnString ;-- lpszReturnString
|
||||
,"UInt",512 ;-- cchReturn
|
||||
,A_PtrSize ? "Ptr":"UInt",p_hwndCallback ;-- hwndCallback
|
||||
,"Cdecl Int") ;-- Return type
|
||||
}
|
||||
; 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
|
||||
36
Task/Record-sound/BBC-BASIC/record-sound.basic
Normal file
36
Task/Record-sound/BBC-BASIC/record-sound.basic
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
wavfile$ = @dir$ + "capture.wav"
|
||||
bitspersample% = 16
|
||||
channels% = 2
|
||||
samplespersec% = 44100
|
||||
|
||||
alignment% = bitspersample% * channels% / 8
|
||||
bytespersec% = alignment% * samplespersec%
|
||||
|
||||
params$ = " bitspersample " + STR$(bitspersample%) + \
|
||||
\ " channels " + STR$(channels%) + \
|
||||
\ " alignment " + STR$(alignment%) + \
|
||||
\ " samplespersec " + STR$(samplespersec%) + \
|
||||
\ " bytespersec " + STR$(bytespersec%)
|
||||
|
||||
SYS "mciSendString", "close all", 0, 0, 0
|
||||
SYS "mciSendString", "open new type waveaudio alias capture", 0, 0, 0
|
||||
SYS "mciSendString", "set capture" + params$, 0, 0, 0 TO res%
|
||||
IF res% ERROR 100, "Couldn't set capture parameters: " + STR$(res% AND &FFFF)
|
||||
|
||||
PRINT "Press SPACE to start recording..."
|
||||
REPEAT UNTIL INKEY(1) = 32
|
||||
|
||||
SYS "mciSendString", "record capture", 0, 0, 0 TO res%
|
||||
IF res% ERROR 100, "Couldn't start audio capture: " + STR$(res% AND &FFFF)
|
||||
|
||||
PRINT "Recording, press SPACE to stop..."
|
||||
REPEAT UNTIL INKEY(1) = 32
|
||||
|
||||
SYS "mciSendString", "stop capture", 0, 0, 0
|
||||
SYS "mciSendString", "save capture " + wavfile$, 0, 0, 0 TO res%
|
||||
IF res% ERROR 100, "Couldn't save to WAV file: " + STR$(res% AND &FFFF)
|
||||
|
||||
SYS "mciSendString", "delete capture", 0, 0, 0
|
||||
SYS "mciSendString", "close capture", 0, 0, 0
|
||||
|
||||
PRINT "Captured audio is stored in " wavfile$
|
||||
93
Task/Record-sound/C++/record-sound.cpp
Normal file
93
Task/Record-sound/C++/record-sound.cpp
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
#include <windows.h>
|
||||
#include <mmsystem.h>
|
||||
|
||||
#pragma comment ( lib, "winmm.lib" )
|
||||
using namespace std;
|
||||
|
||||
class recorder
|
||||
{
|
||||
public:
|
||||
void start()
|
||||
{
|
||||
paused = rec = false; action = "IDLE";
|
||||
while( true )
|
||||
{
|
||||
cout << endl << "==" << action << "==" << endl << endl;
|
||||
cout << "1) Record" << endl << "2) Play" << endl << "3) Pause" << endl << "4) Stop" << endl << "5) Quit" << endl;
|
||||
char c; cin >> c;
|
||||
if( c > '0' && c < '6' )
|
||||
{
|
||||
switch( c )
|
||||
{
|
||||
case '1': record(); break;
|
||||
case '2': play(); break;
|
||||
case '3': pause(); break;
|
||||
case '4': stop(); break;
|
||||
case '5': stop(); return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
private:
|
||||
void record()
|
||||
{
|
||||
if( mciExecute( "open new type waveaudio alias my_sound") )
|
||||
{
|
||||
mciExecute( "record my_sound" );
|
||||
action = "RECORDING"; rec = true;
|
||||
}
|
||||
}
|
||||
void play()
|
||||
{
|
||||
if( paused )
|
||||
mciExecute( "play my_sound" );
|
||||
else
|
||||
if( mciExecute( "open tmp.wav alias my_sound" ) )
|
||||
mciExecute( "play my_sound" );
|
||||
|
||||
action = "PLAYING";
|
||||
paused = false;
|
||||
}
|
||||
void pause()
|
||||
{
|
||||
if( rec ) return;
|
||||
mciExecute( "pause my_sound" );
|
||||
paused = true; action = "PAUSED";
|
||||
}
|
||||
void stop()
|
||||
{
|
||||
if( rec )
|
||||
{
|
||||
mciExecute( "stop my_sound" );
|
||||
mciExecute( "save my_sound tmp.wav" );
|
||||
mciExecute( "close my_sound" );
|
||||
action = "IDLE"; rec = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
mciExecute( "stop my_sound" );
|
||||
mciExecute( "close my_sound" );
|
||||
action = "IDLE";
|
||||
}
|
||||
}
|
||||
bool mciExecute( string cmd )
|
||||
{
|
||||
if( mciSendString( cmd.c_str(), NULL, 0, NULL ) )
|
||||
{
|
||||
cout << "Can't do this: " << cmd << endl;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool paused, rec;
|
||||
string action;
|
||||
};
|
||||
|
||||
int main( int argc, char* argv[] )
|
||||
{
|
||||
recorder r; r.start();
|
||||
return 0;
|
||||
}
|
||||
30
Task/Record-sound/C/record-sound.c
Normal file
30
Task/Record-sound/C/record-sound.c
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
void * record(size_t bytes)
|
||||
{
|
||||
int fd;
|
||||
if (-1 == (fd = open("/dev/dsp", O_RDONLY))) return 0;
|
||||
void *a = malloc(bytes);
|
||||
read(fd, a, bytes);
|
||||
close(fd);
|
||||
return a;
|
||||
}
|
||||
|
||||
int play(void *buf, size_t len)
|
||||
{
|
||||
int fd;
|
||||
if (-1 == (fd = open("/dev/dsp", O_WRONLY))) return 0;
|
||||
write(fd, buf, len);
|
||||
close(fd);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
void *p = record(65536);
|
||||
play(p, 65536);
|
||||
return 0;
|
||||
}
|
||||
23
Task/Record-sound/ChucK/record-sound.chuck
Normal file
23
Task/Record-sound/ChucK/record-sound.chuck
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
// chuck this with other shreds to record to file
|
||||
// example> chuck foo.ck bar.ck rec
|
||||
|
||||
// arguments: rec:<filename>
|
||||
|
||||
// get name
|
||||
me.arg(0) => string filename;
|
||||
if( filename.length() == 0 ) "foo.wav" => filename;
|
||||
|
||||
// pull samples from the dac
|
||||
dac => Gain g => WvOut w => blackhole;
|
||||
// this is the output file name
|
||||
filename => w.wavFilename;
|
||||
<<<"writing to file:", "'" + w.filename() + "'">>>;
|
||||
// any gain you want for the output
|
||||
.5 => g.gain;
|
||||
|
||||
// temporary workaround to automatically close file on remove-shred
|
||||
null @=> w;
|
||||
|
||||
// infinite time loop...
|
||||
// ctrl-c will stop it, or modify to desired duration
|
||||
while( true ) 1::second => now;
|
||||
11
Task/Record-sound/Common-Lisp/record-sound.lisp
Normal file
11
Task/Record-sound/Common-Lisp/record-sound.lisp
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
(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))
|
||||
17
Task/Record-sound/Diego/record-sound-1.diego
Normal file
17
Task/Record-sound/Diego/record-sound-1.diego
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
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
|
||||
18
Task/Record-sound/Diego/record-sound-2.diego
Normal file
18
Task/Record-sound/Diego/record-sound-2.diego
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
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
|
||||
1
Task/Record-sound/GUISS/record-sound.guiss
Normal file
1
Task/Record-sound/GUISS/record-sound.guiss
Normal file
|
|
@ -0,0 +1 @@
|
|||
Start,Programs,Accessories,Sound Recorder,Button:Record
|
||||
61
Task/Record-sound/Go/record-sound.go
Normal file
61
Task/Record-sound/Go/record-sound.go
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
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.")
|
||||
}
|
||||
65
Task/Record-sound/Java/record-sound.java
Normal file
65
Task/Record-sound/Java/record-sound.java
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
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;
|
||||
|
||||
}
|
||||
5
Task/Record-sound/Julia/record-sound.julia
Normal file
5
Task/Record-sound/Julia/record-sound.julia
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
using PortAudio, LibSndFile
|
||||
|
||||
stream = PortAudioStream("Microphone (USB Microphone)", 1, 0) # 44100 samples/sec
|
||||
buf = read(stream, 441000)
|
||||
save("recorded10sec.wav", buf)
|
||||
45
Task/Record-sound/Kotlin/record-sound.kotlin
Normal file
45
Task/Record-sound/Kotlin/record-sound.kotlin
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
// 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)
|
||||
}
|
||||
}
|
||||
1
Task/Record-sound/Liberty-BASIC/record-sound-1.basic
Normal file
1
Task/Record-sound/Liberty-BASIC/record-sound-1.basic
Normal file
|
|
@ -0,0 +1 @@
|
|||
run "sndrec32.exe"
|
||||
34
Task/Record-sound/Liberty-BASIC/record-sound-2.basic
Normal file
34
Task/Record-sound/Liberty-BASIC/record-sound-2.basic
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
print "Starting 5 sec. recording..."
|
||||
r$ =mciSendString$( "open new type waveaudio alias capture")
|
||||
r$ =mciSendString$( "set capture time format ms bitspersample 16")
|
||||
r$ =mciSendString$( "set capture channels 1 samplespersec 8000")
|
||||
r$ =mciSendString$( "set capture alignment 1 bytespersec 8000")
|
||||
r$ =mciSendString$( "record capture")
|
||||
timer 5000, [on]
|
||||
wait
|
||||
[on]
|
||||
timer 0
|
||||
print " .... now stopping the recording."
|
||||
r$ =mciSendString$( "stop capture")
|
||||
r$ =mciSendString$( "save capture " +chr$( 34) +"sample.wav" +chr$( 34))
|
||||
r$ =mciSendString$( "close capture")
|
||||
print "Done recording."
|
||||
r$=mciSendString$( "open " +q$ +"sample.wav" +q$ +" type waveaudio alias sfx")
|
||||
r$=mciSendString$( "play sfx wait")
|
||||
r$=mciSendString$( "close sfx")
|
||||
print "Done playing back."
|
||||
end
|
||||
|
||||
function mciSendString$( s$)
|
||||
print s$
|
||||
buffer$ =space$( 1024) +chr$( 0)
|
||||
calldll #winmm, "mciSendStringA", s$ as ptr, buffer$ as ptr, 1028 as long, 0 as long, r as long
|
||||
buffer$ =left$( buffer$, instr( buffer$, chr$( 0)) -1)
|
||||
if r >0 then
|
||||
mciSendString$ ="error"
|
||||
print "returned "; mciSendString$
|
||||
else
|
||||
mciSendString$ =""'buffer$
|
||||
print "OK"
|
||||
end if
|
||||
end function
|
||||
13
Task/Record-sound/LiveCode/record-sound.livecode
Normal file
13
Task/Record-sound/LiveCode/record-sound.livecode
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
command makeRecording
|
||||
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
|
||||
1
Task/Record-sound/Mathematica/record-sound.math
Normal file
1
Task/Record-sound/Mathematica/record-sound.math
Normal file
|
|
@ -0,0 +1 @@
|
|||
SystemDialogInput["RecordSound"]
|
||||
28
Task/Record-sound/Nim/record-sound.nim
Normal file
28
Task/Record-sound/Nim/record-sound.nim
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import osproc, strutils
|
||||
|
||||
var name = ""
|
||||
while name.len == 0:
|
||||
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"
|
||||
21
Task/Record-sound/OCaml/record-sound.ocaml
Normal file
21
Task/Record-sound/OCaml/record-sound.ocaml
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
#load "unix.cma"
|
||||
|
||||
let record bytes =
|
||||
let buf = String.make bytes '\000' in
|
||||
let ic = open_in "/dev/dsp" in
|
||||
let chunk = 4096 in
|
||||
for i = 0 to pred (bytes / chunk) do
|
||||
ignore (input ic buf (i * chunk) chunk)
|
||||
done;
|
||||
close_in ic;
|
||||
(buf)
|
||||
|
||||
let play buf len =
|
||||
let oc = open_out "/dev/dsp" in
|
||||
output_string oc buf;
|
||||
close_out oc
|
||||
|
||||
let () =
|
||||
let bytes = 65536 in
|
||||
let p = record bytes in
|
||||
play p bytes
|
||||
86
Task/Record-sound/Phix/record-sound.phix
Normal file
86
Task/Record-sound/Phix/record-sound.phix
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
(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>
|
||||
<!--
|
||||
4
Task/Record-sound/PicoLisp/record-sound.l
Normal file
4
Task/Record-sound/PicoLisp/record-sound.l
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
(in '(rec -q -c1 -tu16 - trim 0 2) # Record 2 seconds
|
||||
(make
|
||||
(while (rd 2)
|
||||
(link @) ) ) )
|
||||
17
Task/Record-sound/Python/record-sound.py
Normal file
17
Task/Record-sound/Python/record-sound.py
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import pyaudio
|
||||
|
||||
chunk = 1024
|
||||
FORMAT = pyaudio.paInt16
|
||||
CHANNELS = 1
|
||||
RATE = 44100
|
||||
|
||||
p = pyaudio.PyAudio()
|
||||
|
||||
stream = p.open(format = FORMAT,
|
||||
channels = CHANNELS,
|
||||
rate = RATE,
|
||||
input = True,
|
||||
frames_per_buffer = chunk)
|
||||
|
||||
data = stream.read(chunk)
|
||||
print [ord(i) for i in data]
|
||||
4
Task/Record-sound/Racket/record-sound.rkt
Normal file
4
Task/Record-sound/Racket/record-sound.rkt
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
#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))
|
||||
69
Task/Record-sound/Raku/record-sound.raku
Normal file
69
Task/Record-sound/Raku/record-sound.raku
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
51
Task/Record-sound/Scala/record-sound.scala
Normal file
51
Task/Record-sound/Scala/record-sound.scala
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
import java.io.{File, IOException}
|
||||
import javax.sound.sampled.{AudioFileFormat, AudioFormat, AudioInputStream}
|
||||
import javax.sound.sampled.{AudioSystem, DataLine, LineUnavailableException, TargetDataLine}
|
||||
|
||||
object SoundRecorder extends App {
|
||||
// record duration, in milliseconds
|
||||
final val RECORD_TIME = 60000 // 1 minute
|
||||
|
||||
// path and format of the wav file
|
||||
val (wavFile, fileType) = (new File("RecordAudio.wav"), AudioFileFormat.Type.WAVE)
|
||||
val format = new AudioFormat(/*sampleRate =*/ 16000f,
|
||||
/*sampleSizeInBits =*/ 16,
|
||||
/*channels =*/ 2,
|
||||
/*signed =*/ true,
|
||||
/*bigEndian =*/ true)
|
||||
|
||||
val info = new DataLine.Info(classOf[TargetDataLine], format)
|
||||
val line: TargetDataLine = AudioSystem.getLine(info).asInstanceOf[TargetDataLine]
|
||||
|
||||
// Entry to run the program
|
||||
|
||||
// Creates a new thread that waits for a specified of time before stopping
|
||||
new Thread(new Runnable() {
|
||||
def run() {
|
||||
try {
|
||||
Thread.sleep(RECORD_TIME)
|
||||
} catch {
|
||||
case ex: InterruptedException => ex.printStackTrace()
|
||||
}
|
||||
finally {
|
||||
line.stop()
|
||||
line.close()
|
||||
}
|
||||
println("Finished")
|
||||
}
|
||||
}).start()
|
||||
|
||||
//Captures the sound and record into a WAV file
|
||||
try {
|
||||
// checks if system supports the data line
|
||||
if (AudioSystem.isLineSupported(info)) {
|
||||
line.open(format)
|
||||
line.start() // start capturing
|
||||
println("Recording started")
|
||||
AudioSystem.write(new AudioInputStream(line), fileType, wavFile)
|
||||
} else println("Line not supported")
|
||||
} catch {
|
||||
case ex: LineUnavailableException => ex.printStackTrace()
|
||||
case ioe: IOException => ioe.printStackTrace()
|
||||
}
|
||||
}
|
||||
19
Task/Record-sound/Tcl/record-sound.tcl
Normal file
19
Task/Record-sound/Tcl/record-sound.tcl
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package require sound
|
||||
|
||||
# Helper to do a responsive wait
|
||||
proc delay t {after $t {set ::doneDelay ok}; vwait ::doneDelay}
|
||||
|
||||
# Make an in-memory recording object
|
||||
set recording [snack::sound -encoding "Lin16" -rate 44100 -channels 1]
|
||||
|
||||
# Set it doing the recording, wait for a second, and stop
|
||||
$recording record -append true
|
||||
delay 1000
|
||||
$recording stop
|
||||
|
||||
# Convert the internal buffer to viewable numbers, and print them out
|
||||
binary scan [$recording data -byteorder littleEndian] s* words
|
||||
puts [join $words ", "]
|
||||
|
||||
# Destroy the recording object
|
||||
$recording destroy
|
||||
5
Task/Record-sound/Wee-Basic/record-sound.basic
Normal file
5
Task/Record-sound/Wee-Basic/record-sound.basic
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
print 1 "Recording..."
|
||||
micrec
|
||||
print 1 "Playing..."
|
||||
micpla
|
||||
end
|
||||
39
Task/Record-sound/Wren/record-sound-1.wren
Normal file
39
Task/Record-sound/Wren/record-sound-1.wren
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
/* 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.")
|
||||
102
Task/Record-sound/Wren/record-sound-2.wren
Normal file
102
Task/Record-sound/Wren/record-sound-2.wren
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
#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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue