<tt>
<div dir="ltr"><div>Hello list, I hope this is the right place to ask this.<br><br></div><div>I'm learning Erlang, and I wanted to create a Cowboy app to record audio from a web browser.<br><br>Based on the websocket example in the Cowboy source code, I get the user mic input and send this input to the websocket.<br><br></div><div>I created a "recorder" module, which functionality is to save the data to the a file.<br><br><b>#rawe_handler.erl<br></b>-module(rawec_handler).<br>-behaviour(cowboy_websocket_handler).<br>......<br>init(_, _, _) -><br> case whereis(recorder) of<br> undefined -><br> RecorderPid = recorder:start(),<br> register(recorder, RecorderPid);<br> _ -> ok<br> end,<br> {upgrade, protocol, cowboy_websocket}.<br>.....<br>websocket_handle(_Frame, Req, State) -><br> RecorderPid = whereis(recorder),<br> RecorderPid ! {rec, _Frame/binary},<br> {ok, Req, State}.<br><br></div><div><b>#recorder.erl</b><br>-module(recorder).<br><br>-export([start/0, recorder_fun/1]).<br>-compile([debug_info]).<br><br>recorder_fun(IoDevice) -> <br> receive<br> {rec, Data} -><br> ok = file:write(IoDevice, Data),<br> io:format(Data),<br> recorder_fun(IoDevice);<br> {stop, _} -><br> %%Close file<br> file:close(IoDevice)<br> end.<br> <br>start() -><br> {ok, IoDevice} = file:open("/tmp/test_binary.wav", [write, binary]), <br> spawn(recorder, recorder_fun, [IoDevice]).<br> <br><br></div><div>When I start the console, and allow the microphone on the browser, I see this error on the console:<br><br>=ERROR REPORT==== 29-Sep-2014::18:13:03 ===<br>Ranch listener http had connection process started with cowboy_protocol:start_link/4 at <0.178.0> exit with reason: <b>{[{reason,badarith},{mfa,{rawec_handler,websocket_handle,3</b>}},{stacktrace,[{rawec_handler,websocket_handle,3,[{file,"src/rawec_handler.erl"},{line,35}]},{cowboy_websocket,handler_call,7,[{file,"src/cowboy_websocket.erl"},{line,588}]},{cowboy_protocol,execute,4,[{file,"src/cowboy_protocol.erl"},{line,435}]}]},{msg,{binary,<<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0....(ETC, DATA STREAM CONTINUES)<br></div><div><br></div><div>Probably my approach to do this is totally wrong. I there any obvious problem here?<br></div><div>Can someone point me to a right direction?. Maybe I should write directly to a file in the <b>websocket_handle </b>funcion, but how can I keep a file opened during the streaming?<br><br></div><div>The github repo is here: <a href="https://github.com/jmrepetti/rawec">https://github.com/jmrepetti/rawec</a> with the whole source code if you want to take a look.<br><br><br></div><div>Thanks in advance,<br></div><div>Matias.<br><br>
</div></div><br>
</tt>