Group Abstract Group Abstract

Message Boards Message Boards

Multichannel audio recording?

Posted 2 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

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
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard