2

I need to make a program in Processing that reverses a loaded .mp3 file, avoiding already implemented abstract functions (for example, a Minim function that reverses a given sound, though I still haven't found such). I've tried many ways by now and it still doesn't play.

My last approach uses the Minim.loadFileIntoBuffer() function to get a float[] and then I reverse the buffer using Processing's reverse() function. I then create an AudioSample with the same float array and the original file's format. I then proceed to play the sample using trigger().

Here's a snippet:

Minim minim;
AudioPlayer songPlayer;
MultiChannelBuffer song;
AudioSample reversedSong;
float[] reversedBuffer, mixBuffer;

void setup() {
  size(512, 200);
  minim = new Minim(this);

  songPlayer = minim.loadFile("sound.mp3");

  // loadFileIntoBuffer changes the MultiChannelBuffer's parameters to
  // match the file's, so init values doesn't really matter
  song = new MultiChannelBuffer(10, 3);
  minim.loadFileIntoBuffer("sound.mp3", song);

  mixBuffer = songPlayer.mix.toArray();
  reversedBuffer = reverse(mixBuffer);

 reversedSong = minim.createSample(reversedBuffer, songPlayer.getFormat());
 reversedSong.trigger();
} 

I've also tried some different solutions. Namely importing directly the file into a AudioSample. The problem with most ways I've already used, I believe, is that I can only access an AudioBuffer, i.e. a part of the whole sound. Thus, I cannot reverse it all.

Do you know any way of doing this?

flapas
  • 583
  • 1
  • 11
  • 25

1 Answers1

1

Ok I eventually managed to do it, by a simpler process.

I get the original sample's channels and I directly manipulate them. First, construct a new floatarray with the same size as the channels. Then, put the values back in the original using the same procedure.

It's far from being a beautiful implementation and it's probably more time-complex then it could be. But it does the trick, since I was limited to using no implemented high level functions.

void revert() {
  /* sample is an AudioSample, created in setup() */
  float[] leftChannel = sample.getChannel(AudioSample.LEFT);
  float[] rightChannel = sample.getChannel(AudioSample.RIGHT);

  float[] leftReversed = new float[leftChannel.length];
  float[] rightReversed = new float[rightChannel.length];

  int size = leftChannel.length-1;

  /*inverts the array*/
  for(int i=0; i<=size; i++){
    leftReversed[i] = leftChannel[size-i];
    rightReversed[i] = rightChannel[size-i];
  }
  /*copies it to the original array*/
  for(int i=0; i<size; i++){
    leftChannel[i] = leftReversed[i];
    rightChannel[i] = rightReversed[i];
  }  
}
flapas
  • 583
  • 1
  • 11
  • 25