Is there a way to play a SoundFile muted and keep its amplitude data?

Trying to gather data from a sound file without listening to it, but I’m having trouble getting there. I thought I might be able to do it by playing the file in draw(), gathering the FFT spectrum data, then pausing at the end.

I tried the reverse order first to make sure the sound is working properly, but the results were strange. I thought that draw() would only render the final state of the file, so if I put play last, it would just play it, but it seems every draw() frame it pauses and plays, chopping up and slowing down the sample in half. Below is an example of the code I’m using with sin440.wav as a stand-in for whatever sound file you want to use.

import processing.sound.*;

SoundFile file;

void setup() {
  size(200, 200);
  background(255);

  
  String audio ="sin440.wav";
  file = new SoundFile(this, audio);

}

void draw() {
if (file.isPlaying()){
  file.pause();

}
if (!file.isPlaying()){
  file.play();

}


}
1 Like

i think

/ Files / Examples / Minim / Analysis / FFT / offlineAnalysis

does what you try to do on a sound file.


but it is a very complex example!

2 Likes

Thanks for the research. I haven’t used the minim library yet. I’ll look into it.

Do you know by any chance why play/pause acts this way in the above example?

For that I would recommend minim – but if you want to do it with the Sound library, you should start by working with the FFT analysis example:

Starting point suggestions: consider how Sound enables you to map input-output devices, or how fft.input allows you to patch a source.

Could you elaborate on how I might solve this problem with the use of mapping input/output devices?

With regards to FFT.input, it seems like it only analyzes when .play() is active. Is that a correct observation? That’s what I’ve tried to work with in the sound library. Play the sound, get the FFT spectrum, Pause, next draw frame, repeat. However, all combinations and methods I tried with that library so far have either resulted in Play being called again and again, overloading the soundcard, or a stuttery/clicky pause-play-pause-play, etc. that halves the speed of the source. Can’t get one draw frame to Play and Pause smoothly in one go.

My guess right now is that .play() executes at the end of a draw frame no matter where it’s placed in the code. Also that each draw frame creates its own instance of .play/pause/stop() and the next instance can’t interact with the previous. Do you know if I got any of that right? haha.

may i draw that in a cartoon?

you sit in your armchair, and shout!

darling, switch ON the TV

darling, switch OFF the TV

and you do that 60 times per second!
what you will get?

divorce


actually what you try to do should not involve SOUND stream at all,
what you need would be to read the data from file
( and that i do not know how to do from today sound files )
in 512 array batches and run your own FFT on that array.
and on keyPressed read the next 512 lines.

Haha, nice. Yeah, if I knew how to just read it from the file I would, unfortunately I’m an amateur who struggles with things I assume pros could do in a couple seconds.

For now my solution was just to lower the amp to an inaudible amount and just use the map function when I need to use the data.

i show you the way??

Yes, but like you said, it’s quite complicated. For now, lowering the amp is a simpler solution for me.

Thanks kll.

I honestly don’t know – it might be possible, which is why I suggested it as a starting point, but I can only recall seeing minim actually used for this, which is why I recommended that instead.