Group Abstract Group Abstract

Message Boards Message Boards

Multichannel audio recording?

Posted 6 months ago
POSTED BY: Daniel Warren
2 Replies

There is no native way to do that in M (sounds like an easy addition for the next version though!). In the meantime, if you're willing to use some hacks, this should do the trick:

(*Load the relevant code*)
$AudioInputDevices;
(*hack a downvalue*)
FFmpegTools`Audio`Private`validateParameter[head_, "ChannelCount", value_] :=
If[!(Internal`PositiveIntegerQ[value] && 1 <= value),
    `LLU`ThrowPacletFailure["InvalidChannelCount", "MessageParameters" -> <|"ChannelCount" -> value|>]
];

SetAttributes[recordDevice, HoldRest];
recordDevice[deviceName_, numChannels_, bagOfData_] :=
    Module[
       {t},
       bagOfData = {};
       t = FFmpegTools`Audio`CreateAudioCaptureAsynchronousTask[
         deviceName, Function[AppendTo[bagOfData, #3]],
         "ChannelCount" -> numChannels
       ];
       FFmpegTools`Audio`StartAudioAsynchronousTask[t];
       t
    ];
stopRecording[t_] := (FFmpegTools`Audio`StopAudioAsynchronousTask[t]; FFmpegTools`Audio`RemoveAudioAsynchronousTask[t]);
constructAudio[bagOfData_] := Audio[Transpose@Flatten[bagOfData, {1, 3}], "SignedInteger16"]

And then use the functions:

In[16]:= bag = {};
task = recordDevice["MultiInput", 5, bag]

Out[17]= AsynchronousTaskObject[<|
 "Type" -> "AudioDevice", "SubType" -> "InputDevice", "DeviceName" -> 
  "MultiInput", "ConnectionID" -> 105553163878432, "Mode" -> 
  "Continuous"|>, 3, 20812609761764629468]

In[18]:= stopRecording[task]
constructAudio[bag] // AudioChannels

Out[19]= 5
POSTED BY: Carlo Giacometti
Posted 6 months ago

Carlo,

Thanks. It will take me some time to build up my test system to the point that I can fully explore this solution and I will get back to the forum on my results.

On the "easy addition to next version" point, I think this would be a powerful addition to WL. Wolfram had added a lot a capability for data acquisition and system control with microprocessors several versions ago, but data acquisition through audio interfaces (beyond simple recording through the system microphone) is lacking in the core language. All the pieces are already built in for measurement and analysis of system transfer functions except synchronized, multichannel stimulus/response measurements and a couple of tutorials. This space is currently dominated by LabView, Matlab, and python but I see substantial inroads into the engineering community with a few simple additions to the language.

And yes, I am aware that I have access to python through External Evaluation, but I already know WL, why should I also learn python?

Daniel

POSTED BY: Daniel Warren
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard