Record sound: Difference between revisions
Content deleted Content added
Added Wren |
m →{{header|Phix}}: syntax coloured, marked p2js incompatible |
||
Line 528: | Line 528: | ||
{{trans|C}} |
{{trans|C}} |
||
{{trans|Go}} |
{{trans|Go}} |
||
<lang Phix>-- |
<!--<lang Phix>(notonline)--> |
||
<span style="color: #000080;font-style:italic;">-- |
|||
constant wavfile = "capture.wav", |
|||
-- demo\rosetta\Record_sound.exw |
|||
bitspersample = 16, |
|||
-- ============================= |
|||
channels = 2, |
|||
--</span> |
|||
samplespersec = 44100, |
|||
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (file i/o)</span> |
|||
alignment = bitspersample * channels / 8, |
|||
<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> |
|||
bytespersec = alignment * samplespersec, |
|||
<span style="color: #000000;">bitspersample</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">16</span><span style="color: #0000FF;">,</span> |
|||
params = sprintf(" bitspersample %d channels %d alignment %d samplespersec %d bytespersec %d", |
|||
<span style="color: #000000;">channels</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> |
|||
{bitspersample, channels, alignment, samplespersec, bytespersec}), |
|||
<span style="color: #000000;">samplespersec</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">44100</span><span style="color: #0000FF;">,</span> |
|||
error_size = 2048 |
|||
<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> |
|||
atom winmm = NULL, xmciSendString, pError |
|||
<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> |
|||
procedure mciSendString(string msg) |
|||
<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> |
|||
if winmm=NULL then |
|||
<span style="color: #000000;">error_size</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2048</span> |
|||
winmm = open_dll("winmm.dll") |
|||
<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> |
|||
xmciSendString = define_c_func(winmm,"mciSendStringA", |
|||
{C_PTR, -- LPCTSTR lpszCommand |
|||
C_PTR, -- LPTSTR lpszReturnString |
|||
C_INT, -- UINT cchReturn |
|||
C_PTR}, -- HANDLE hwndCallback |
|||
C_INT) -- MCIERROR |
|||
pError = allocate(error_size) |
|||
end if |
|||
atom res = c_func(xmciSendString,{msg,pError,error_size,NULL}) |
|||
if res!=0 then crash("error %0x: %s",{res,peek_string(pError)}) end if |
|||
end procedure |
|||
include get.e -- get_bytes() |
|||
function record(integer bytes) |
|||
integer fn = open("/dev/dsp","rb") |
|||
if fn=-1 then return "" end if |
|||
string res = get_bytes(fn,bytes) |
|||
close(fn) |
|||
return res |
|||
end function |
|||
<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> |
|||
procedure play(string buf) |
|||
<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> |
|||
if length(buf) then |
|||
<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> |
|||
integer fn = open("/dev/dsp","wb") |
|||
<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> |
|||
if fn!=-1 then |
|||
<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> |
|||
puts(fn,buf) |
|||
<span style="color: #000000;">C_PTR</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- LPTSTR lpszReturnString</span> |
|||
close(fn) |
|||
<span style="color: #000000;">C_INT</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- UINT cchReturn</span> |
|||
end if |
|||
<span style="color: #000000;">C_PTR</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- HANDLE hwndCallback</span> |
|||
end if |
|||
<span style="color: #000000;">C_INT</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- MCIERROR</span> |
|||
end procedure |
|||
<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> |
|||
if platform()=WINDOWS then |
|||
<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> |
|||
mciSendString("close all") |
|||
<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> |
|||
mciSendString("open new type waveaudio alias capture") |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span> |
|||
mciSendString("set capture" & params) |
|||
puts(1,"Press SPACE to start recording...") |
|||
while wait_key()!=' ' do end while |
|||
<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> |
|||
mciSendString("record capture") |
|||
puts(1,"Recording, press SPACE to stop...") |
|||
while wait_key()!=' ' do end while |
|||
<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> |
|||
mciSendString("stop capture") |
|||
<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> |
|||
mciSendString("save capture " & wavfile) |
|||
<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> |
|||
mciSendString("delete capture") |
|||
<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> |
|||
mciSendString("close capture") |
|||
<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> |
|||
puts(1,"Captured audio is stored in "&wavfile) |
|||
<span style="color: #000000;">mciSendString</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"close all"</span><span style="color: #0000FF;">)</span> |
|||
elsif platform()=LINUX then |
|||
<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> |
|||
-- warning: untested |
|||
<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> |
|||
play(record(65536)) |
|||
-- -- alternative, from Go (ditto) |
|||
<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> |
|||
-- string name = "test.wav", |
|||
<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> |
|||
-- rate = "2000", -- (2000..192000 Hz) |
|||
-- durn = "5" -- (5 to 30 seconds) |
|||
<span style="color: #000000;">mciSendString</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"record capture"</span><span style="color: #0000FF;">)</span> |
|||
-- printf(1,"OK, start speaking now...\n") |
|||
<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> |
|||
-- -- Default arguments: -c 1, -t wav. Note only signed 16 bit format supported. |
|||
<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> |
|||
-- string cmd = sprintf("arecord -r %s -f S16_LE -d %s %s", {rate,durn,name}) |
|||
-- {} = system_exec(cmd) |
|||
<span style="color: #000000;">mciSendString</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"stop capture"</span><span style="color: #0000FF;">)</span> |
|||
-- printf(1,"'%s' created on disk and will now be played back...\n", {name}) |
|||
<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> |
|||
-- {} = system_exec("aplay "&name) |
|||
<span style="color: #000000;">mciSendString</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"delete capture"</span><span style="color: #0000FF;">)</span> |
|||
-- printf(1,"Play-back completed.\n") |
|||
<span style="color: #000000;">mciSendString</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"close capture"</span><span style="color: #0000FF;">)</span> |
|||
end if</lang> |
|||
<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> |
|||
<!--</lang>--> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |