# Multiple input audio devices

**URL:** <https://discourse.processing.org/t/multiple-input-audio-devices/43239>\
**Category:** Libraries\
**Created:** [November 18, 2023, 1:24am UTC](https://discourse.processing.org/t/multiple-input-audio-devices/43239 "2023-11-18T01:24:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pandela](https://avatars.discourse-cdn.com/v4/letter/p/8797f3/32.png) [@Pandela](https://discourse.processing.org/u/Pandela)\
**Post date:** [November 18, 2023, 1:24am UTC](https://discourse.processing.org/t/multiple-input-audio-devices/43239/1 "2023-11-18T01:24:13Z")

</div>

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 😃  
Inspired by [this post](https://forum.processing.org/two/discussion/3109/how-to-get-multiple-audio-outputs-with-minim.html)

```auto

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);
}

```

---

<div class="post-metadata">

**Author:** ![sterretje](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@sterretje](https://discourse.processing.org/u/sterretje)\
**Post date:** [November 21, 2023, 8:39am UTC](https://discourse.processing.org/t/multiple-input-audio-devices/43239/2 "2023-11-21T08:39:06Z")

</div>

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.
