I'm trying to save a recording using AudioRecord
. My recording gets overwritten even if I save the output in a fresh cell. This illustrates the issue:
astream = AudioStream[First@$AudioInputDevices];
sample = AudioRecord[astream];
DialogInput[Print["Record now for 5 s..."]; Pause[5];
Print["Recorded ~ 5 s"]; DialogReturn["Close me"]];
AudioStop[astream];
player = Audio[sample]
My recording is returned as an output as shown. I save it to a new cell.
Now I do a new recording on the same channel:
AudioRecord[astream];(* once again *)
DialogInput[Print["Record again now for 10 s..."]; Pause[10];
Print["Recorded ~ 10 s"]; DialogReturn[]];
AudioStop[astream];
player
Now the recording above looks the same (this does not always happen though; sometimes a fresh audio gadget replaces the one I saved before) but the data has been overwritten with the first 5 s of the new recording.
This looks like a bug to me although I have an intuition as to why this is happening (Dynamic
); can anyone suggest a workaround?
A more fundamental issue is that it does not seem to be possible to close the audio stream or to dispose of the resources it is using (without quitting the kernel). That's what's familiar to me from using streams in other programming languages. Is it too much to hope this might be available in future?
Export
it to a file? $\endgroup$ExportString
? $\endgroup$reconstructedPlayer = Audio[ImportString[ExportString[player = Audio[sample], "MP3"], "MP3"]]
would do it nicely. $\endgroup$RemoveAudioStream
, and each time you record create a newAudioStream
. This will guarantee that previous recordings will never be overwritten. $\endgroup$