Group Abstract Group Abstract

Message Boards Message Boards

Multichannel audio recording?

Posted 12 days ago

Is it possible to make multichannel audio recordings (beyond stereo L/R) in Mathematica? I have a 4-channel audio interface ("Listen AudioConnect 4x4") attached but am only getting single channel results.

Context: As a retirement project I am making a test system for loudspeakers in which I make simultaneous measurements of response at different sensors (sound pressure, voltage, current, displacement) to a stimulus generated in Mathematica and synchronously played back on the same audio interface. I have done this in the past prior to the introduction of Audio, but it required using a Matlab toolbox (and MatLink, no longer supported) for the data acquisition. In the new system I would like to do this entirely within Mathematica, but I can't seem to get enough input data channels to record.

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 8 days 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