Chad Schultz
Chad Schultz

Reputation: 7860

Editing audio / rerecording part of a file

I want the user to be able to record audio in an app, and then to be able to select part of the file and rerecord it. For example, perhaps they initially record "How now, brown cow," then select the end of that and rerecord "frown sow." This would be merged into the initial file, which would then be "How now, frown sow."

This seems to be far more difficult than I expected. Can I do this with 3GP format files? If not, how can I convert them to the format I need?

I want to be able to say "take this just-recorded audio file. It will fill from the 5-second to the 10-second section of this other media file, now save the new, edited file." I gather converting between milliseconds, frames, samples and bytes could make this even more complicated.

I'm examining the Ringdroid app (I'm SO thankful it's open source!) which at least does something similar, but is very complicated with little in the way of comments... hopefully it's much more complicated than I need.

I did see this other thread, but even if it was what I needed, I don't believe those classes are a part of Android: Saving part of an audio file (Java)

Upvotes: 2

Views: 2976

Answers (1)

Chad Schultz
Chad Schultz

Reputation: 7860

I was finally able to edit parts of a 3GP file.

I used the CheapAMR (which also manages 3GP) and CheapSoundFile classes from RingDroid, as well as a new "AudioFileHandler" class.

I learned some important lessons along the way:

  • a 3GP file is divided into "boxes." Each box contains a value stating how long the box is, and then some data. There's an "ftyp" box with metadata about the 3GP file, a "mdat" box with the actual music data, and a "moov" box with movie data (even audio-only files have this box."
  • the order of the boxes in the file, and their respective size, changes from Android device to Android device
  • the music data itself is divided into "frames" (32 bytes in my testing with 3GP, but I gather it could potentially be different lengths). Change a file by replacing or adding a frame, never by editing bytes of a frame.
  • hex editors are very useful tools when doing this kind of coding
  • RingDroid has some bugs; it does not accurately report the number of frames, etc, so hacks have to be written to work around this
  • when changing the length of the audio file, make sure to leave the ftyp and moov boxes untouched, and change the four bytes immediately preceding "mdat" in the file. This represents the length of the mdat box in bytes, including the eight bytes of the mdat header itself.

Upvotes: 5

Related Questions