Multiple input audio devices

Recently I was messing around with Minum and found a way to read multiple virtual audio devices, in Processing 4.3 on Windows 10. Is there a more efficient way I can do this? I’m really only interested in the volume/amplitude and just want accurate numbers in realtime :smiley:
Inspired by this post


import ddf.minim.*;
import javax.sound.sampled.*;

Minim minim1, minim2, minim3;
AudioInput input1, input2, input3;
Mixer.Info[] mixerInfo;

void setup() {
  size(512, 200, P3D);

  minim1 = new Minim(this);
  minim2 = new Minim(this);
  minim3 = new Minim(this);
  
  int index1 = getMixerIndexByName("Line 1 (Virtual Audio Cable)"); // replace with actual mixer name
  int index2 = getMixerIndexByName("Line 2 (Virtual Audio Cable)"); // replace with actual mixer name
  int index3 = getMixerIndexByName("Line 3 (Virtual Audio Cable)"); // replace with actual mixer name
  println(index1);
  println(index2);
  println(index3);

  mixerInfo = AudioSystem.getMixerInfo();
  for (int i = 0; i < mixerInfo.length; i++) {
    println(i + " = " + mixerInfo[i].getName());
  }


  Mixer mixer1 = AudioSystem.getMixer(AudioSystem.getMixerInfo()[index1]);
  minim1.setInputMixer(mixer1);
  input1 = minim1.getLineIn();

  Mixer mixer2 = AudioSystem.getMixer(AudioSystem.getMixerInfo()[index2]);
  minim2.setInputMixer(mixer2);
  input2 = minim2.getLineIn();

  Mixer mixer3 = AudioSystem.getMixer(AudioSystem.getMixerInfo()[index3]);
  minim3.setInputMixer(mixer3);
  input3 = minim3.getLineIn();
}

int getMixerIndexByName(String name) {
  Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
  for (int i = 0; i < mixerInfo.length; i++) {
    if (mixerInfo[i].getName().equals(name)) {
      println("Mixer found at index: " + i);
      return i;
    }
  }
  println("Audio device not found: " + name);
  return -1; // Return -1 if the mixer is not found
}

void draw() {
  background(0); // Clear the background

  // Print RMS volume for each input
  printRMSVolume("Input 1", input1);
  printRMSVolume("Input 2", input2);
  printRMSVolume("Input 3", input3);
}

void printRMSVolume(String label, AudioInput input) {
  // Get the mixed (mono) buffer
  float[] buffer = input.mix.toArray();
  
  // Calculate the RMS of the buffer
  float sum = 0;
  for (int i = 0; i < buffer.length; i++) {
    sum += buffer[i] * buffer[i]; // Square each value and add to sum
  }
  float rms = sqrt(sum / buffer.length);
  
  // Print the RMS value
  println(label + " Volume (RMS): " + rms);
}

From a programming perspective, I would use an array / arrayList of mixers so you can get rid of mixer1, mixer2 … mixerN. Same for inputN and minimumN.

Because a mixer, a minimum and an input belong together, I would combine them in a class and create an array / arrayList of objects of that new class and work with that.