Record sound: Difference between revisions
({{header|GUISS}}) |
(alphabetical order of the examples) |
||
Line 6: | Line 6: | ||
=={{header|C}}== |
=={{header|C}}== |
||
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. |
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. |
||
<lang c>#include <stdlib.h> |
<lang c>#include <stdlib.h> |
||
#include <unistd.h> |
#include <unistd.h> |
||
Line 37: | Line 39: | ||
return 0; |
return 0; |
||
}</lang> |
}</lang> |
||
⚫ | |||
⚫ | |||
⚫ | |||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
<lang PicoLisp>(in '(rec -q -c1 -tu16 - trim 0 2) # Record 2 seconds |
<lang PicoLisp>(in '(rec -q -c1 -tu16 - trim 0 2) # Record 2 seconds |
||
(make |
(make |
||
(while (rd 2) |
(while (rd 2) |
||
(link @) ) ) )</lang> |
(link @) ) ) )</lang> |
||
Output: |
Output: |
||
<pre>-> (16767 19071 17279 ... 5503 9343 14719) # 96000 numbers</pre> |
<pre>-> (16767 19071 17279 ... 5503 9343 14719) # 96000 numbers</pre> |
||
=={{header| |
=={{header|Python}}== |
||
⚫ | |||
⚫ | |||
⚫ | |||
<lang python>import pyaudio |
<lang python>import pyaudio |
||
Line 72: | Line 78: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
{{libheader|Snack}} |
{{libheader|Snack}} |
||
<lang tcl>package require sound |
<lang tcl>package require sound |
||
Revision as of 22:12, 1 November 2011
You are encouraged to solve this task according to the task description, using any language you may know.
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.)
C
Read/write raw device /dev/dsp
. On Linux you need access to said device, meaning probably you should be in audio user group.
<lang c>#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; }</lang>
GUISS
Here we activate the Microsoft Windows '95 Sound Recorder:
<lang guiss>Start,Programs,Accessories,Sound Recorder,Button:Record</lang>
PicoLisp
<lang PicoLisp>(in '(rec -q -c1 -tu16 - trim 0 2) # Record 2 seconds
(make (while (rd 2) (link @) ) ) )</lang>
Output:
-> (16767 19071 17279 ... 5503 9343 14719) # 96000 numbers
Python
<lang python>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]</lang>
Tcl
<lang tcl>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</lang>