mattdiamond / Recorderjs

A plugin for recording/exporting the output of Web Audio API nodes
4.16k stars 1.46k forks source link

deleting portion of audio #153

Open daversm opened 8 years ago

daversm commented 8 years ago

Hi,

I am wondering, is there a way to delete just a portion of the audio recording, something like rec.clear() , but the ability to clear just a portion of the buffer.

Thanks

EddFigueiredo commented 8 years ago

I would like to do something similar... Is there a way, to start a recording, pause it or stop it, select a period like, 50 seconds or whatever, and start recording again from this period over?

daversm commented 8 years ago

I have a possible solution for this, its what I ended up using, does not require any alteration to the Recorderjs api.

So lets say you have a recording, but now wish to start recording again from the 50sec mark based on the previous recording. One way you can do this is, when you record the first time, export the audio buffers( using getBuffers() ) and save them. Now do a recording a second time, once again export the buffers and save them. But because we wanted to start the second recording from the 50sec mark on the first recording, we have to take recording 1 buffers, delete everything after the index that corresponds the 50sec mark. Then its just a matter of joining your edited recording 1 buffers to your recording 2 buffers.

This will essentially do what you are asking.

As far as finding the index that corresponds to X seconds, that would be: index = X * sampleRate.

So if I wanted to do two recordings and start the second recording at the 50sec mark.

recOne = recordingOneBuffers; recTwo = recordingTwoBuffers;

editedRecOne = recordingOneBuffers.slice(0, 50* sampleRate);

finalRec = concat editedRecOne and recTwo;

*Note Recorderjs will export audio buffers in the form off float32arrays, so you will have to implement your own concat function, like so:

function Float32Concat(first, second){ var firstLength = first.length, result = new Float32Array(firstLength + second.length);

  result.set(first);
  result.set(second, firstLength);

  return result;

}

*Final Notes, using a bit more math you can be more precise when finding the index that corresponds to the X sec mark. This is just the solution I ending up using, I'm not sure how efficient this is with respect to time and space.

EddFigueiredo commented 8 years ago

I'll try to implement this solution! It makes sense... I was investigating the data from those float32arrays and trying to find exactly the corresponding value per time!

Thank you!

daversm commented 8 years ago

No prob, good luck