Audio Scripting - inputLevelChanged & inputReceived [Solved]


#1

Hi! Forgive a newbish question, but I’m fairly new to JS (having primarily coded in C# for Unity development) and I’m having trouble trying to access information from a couple of API calls in the Audio namespace. Specifically, I’m trying to get output from the inputLevelChanged and inputReceived functions but have at best only received “undefined” messages so far. I can tell I’m going about accessing them incorrectly & I’m giving myself a crash course in JS to wrap my head around syntax. In the meantime, if any of you wonderful folks would care to enlighten me as to how to go about getting proper output from these calls and assign them to my own variables, it would be greatly appreciated. Thanks!

I’ve tried it a few ways, but here’s the latest example of my broken code:

var sound = SoundCache.getSound("atp:/sounds/test.mp3");
var injector;

Script.setTimeout(function () {
  injector = Audio.playSound(sound); 
}, 1000);

var level;
var currentLevel;
var input = [];

function updateAudioInfo(){    
    currentLevel = Audio.inputLevelChanged(level);
    print("Level: " + currentLevel);
    // print("Current: " + Audio.inputLevelChanged(level));
    /*      
    input = Audio.inputReceived(input);
    var i;
    for (i = 0; i < input.length; i++) {
      print("Input [" + i + "]: " + input[i]);
    } 
    */   
}

Script.update.connect(updateAudioInfo);

ETA: I suppose what’s flummoxing me most is that it seems to want input from me of the exact type that I’m looking for as output. If I already had the information to supply, I wouldn’t be querying the silly thing, lol! :stuck_out_tongue: Again, pretty sure I’m just misunderstanding something fundamental here.


#2

Hi. The Audio API’s inputLevelChanged() is a “signal” so you want to connect it (not Script.update - which is a different signal) to a function of yours. I.e., something along the lines of …

function onAudioInputLevelChanged(level) {
    print("Level: " + level);
}

Audio.inputLevelChanged.connect(onAudioInputLevelChanged);

Similarly, the Audio API’s inputReceived() is a signal, so it needs to be connected to a function of yours.


#3

Thanks so much @ctrlaltdavid! That certainly sheds some light on things. Unfortunately, while my code is now error free it still doesn’t seem to be functioning as I’d expected. Perhaps I’m misinterpreting the nature of the API calls. I’d assumed that, once an audio clip was playing, the inputLevelChanged and inputReceived functions (once connected) would be called and I’d see the information. My audio loads fine, but that’s the extent of it. Is there something else I need to tie in, or am I just misunderstanding how these calls are supposed to work?

Here’s my current test code, just looking for output from those two functions:

var sound = SoundCache.getSound("atp:/sounds/test.mp3");
var injector;

Script.setTimeout(function () {
  injector = Audio.playSound(sound); 
}, 1000);

function onAudioInputLevelChanged(level) {
  print("Level: " + level);
}

function onAudioInputReceived(inputSamples) {
  var i;
  for (i = 0; i < inputSamples.length; i++) {
    print("Input [" + i + "]: " + inputSamples[i]);
  } 
}

Audio.inputLevelChanged.connect(onAudioInputLevelChanged);
Audio.inputReceived.connect(onAudioInputReceived);

I’ll keep tinkering. Maybe I’ll have a good “Eureka!” moment :smiley: Thanks again for the assistance!


#4

Looking through the API a bit further, I finally realized that the “input” I’ve been trying to play with is actually the user’s mic and not the audio stream I thought I was trying to access. Doh! Ran the code with my mic unmuted and the Level information started printing out. Nothing from the inputReceived, oddly, but it wasn’t the information I was actually after anyway… I’ll keep digging and see if I can find similar info from the domain audio instead. Woohoo, I’ve learned a bit today! Thanks yet again! :smiley:

Updated: Aha! Looks like all I needed was to getLoudness from the injector that was playing the mp3. I don’t see any way to get sample information out of the current audio track, but I suppose I can work with loudness levels alone to make something rudimentary. Back to my mad science!