2

I want to change the tempo of mp3 files without changing the pitch. I have byte array of mp3 file , want to change its tempo and save it with that increased or decreased tempo in sdcard. I searched about it alot , people were taking about Lame library and ffmpeg. Is it possible to change tempo with lib ffmpeg or lib Lame of a audio mp3 file in android.If yes then how to compile ffmpeg or Lame on windows. need help in writing android.mk and wrapper class. Help me out Thanks

EDIT

I have successfully compiled lib lame with help of this Link. but still don't know how to use those native methods(written in Encoder.java file) to change the tempo of mp3 file. As am new to android need right direction to move into right direction to solve my problem. I have seen a application named xSpeedPlayer on android market. This app changes the tempo of mp3 and saves that new file into sdcard . This is what i want to achieve in my app too. Then i searched how they are achieving this in xSpeedPlayer and found that they are using mp3Lame and mpg123 library. But don't know how they have done it using these libs. Now i need help to move forward .

EDIT

I have pcm data of Mp3 file and have native mathods of LAME lib which are.. (But dont know how to use these Methods to change tempo)

/**
 * Initialize LAME.
 * 
 * @param inSamplerate input sample rate in Hz.
 * @param outChannel number of channels in input stream.
 * @param outSamplerate output sample rate in Hz.
 * @param outBitrate brate compression ratio in KHz.
 * @param quality quality=0..9. 0=best (very slow). 9=worst.<br />
 *            recommended:<br />
 *            2 near-best quality, not too slow<br />
 *            5 good quality, fast<br />
 *            7 ok quality, really fast
 * @param id3tagTitle ID3 Tag title.
 * @param id3tagArtist ID3 Tag artist.
 * @param id3tagAlbum ID3 Tag album.
 * @param id3tagYear ID3 Tag year.
 * @param id3tagComment ID3 Tag comment.
 */
public native static int init(int inSamplerate, int outChannel,
        int outSamplerate, int outBitrate, int quality, String id3tagTitle,
        String id3tagArtist, String id3tagAlbum, String id3tagYear,
        String id3tagComment);

/**
 * Encode buffer to mp3.
 * 
 * @param instanceIndex Instance index.
 * @param buffer_l PCM data for left channel.
 * @param buffer_r PCM data for right channel.
 * @param sambles number of samples per channel.
 * @param mp3buf result encoded MP3 stream. You must specified
 *            "7200 + (1.25 * samples)" length array.
 * @return number of bytes output in mp3buf. Can be 0.<br />
 *         -1: mp3buf was too small<br />
 *         -2: malloc() problem<br />
 *         -3: lame_init_params() not called<br />
 *         -4: psycho acoustic problems
 */
public native static int encode(int instanceIndex, short[] buffer_l, short[] buffer_r,
        int samples, byte[] mp3buf);

/**
 * Encode buffer L & R channel data interleaved to mp3.
 * 
 * @param instanceIndex Instance index.
 * @param pcm PCM data for left and right channel, interleaved.
 * @param sambles number of samples per channel. <strong>not</strong> number
 *            of samples in pcm[].
 * @param mp3buf result encoded MP3 stream. You must specified
 *            "7200 + (1.25 * samples)" length array.
 * @return number of bytes output in mp3buf. Can be 0.<br />
 *         -1: mp3buf was too small<br />
 *         -2: malloc() problem<br />
 *         -3: lame_init_params() not called<br />
 *         -4: psycho acoustic problems
 */
public native static int encodeBufferInterleaved(int instanceIndex, short[] pcm, int samples,
        byte[] mp3buf);

/**
 * Flush LAME buffer.
 * 
 * @param instanceIndex Instance index.
 * @param mp3buf result encoded MP3 stream. You must specified at least 7200
 *            bytes.
 * @return number of bytes output to mp3buf. Can be 0.
 */
public native static int flush(int instanceIndex, byte[] mp3buf);

/**
 * Close LAME.
 * 
 * @param instanceIndex Instance index.
 */
public native static void close(int instanceIndex);

}

Is it possible to change the tempo of raw data of mp3 and store it in sdcard ? Help me out ... Thanx for any kind of help

Harsh
  • 346
  • 7
  • 19
  • 1
    What code have you already written? Where did building ffmpeg or lame break on windows? What API methods in these libraries are you using but not getting the expected results? – tkone Aug 23 '12 at 11:40
  • 1
    @tkone hi ... i have edited my question with the work i have done. – Harsh Aug 23 '12 at 12:26
  • 1
    as far as I know, the result will be of much less quality than you may expect, since you cannot adjust the sampling rate of the source (that has to be done while recording). Funny fact: In this case vinyl discs are superior to CD/wave/... because they contain analog data and are not that vulnerable to reduced sampling rate. – sschrass Aug 29 '12 at 08:22

1 Answers1

2

imho, your issue is not directly related to ffmpeg, but has to do with signal processing.

In fact, I would cut your needs into 3 parts : 1- you need to extract the raw data from your mp3 file 2- you need to apply some signal processing algorithm on these data to change pitch (interesting and difficult task) 3- you need to convert the new computed datas to mp3, then same them on SDCard.

Part 1 and 3 should not be very difficult to be resolved with some Googling.

About Part 2, you need to have some knowledge about signal processing basics. What I would try is to apply a Fast-Fourier Transform on you data, then scale the results on the time-scale to change your tempo, and finally perform the inverse FFT. That's the only way (afaik) to change tempo without modifying the pitch.

Hope this helps...

Orabîg
  • 11,718
  • 6
  • 38
  • 58
  • 1
    Thanx for your responce. I have extracted the pcm data from mp3 file . Now according to you i should apply FFT to my raw data . Can you suggest some helpful links or tutorials , by which i can learn how to apply FFT , time scaling and reverse FFT . Thanx for your help – Harsh Aug 28 '12 at 15:01
  • 1
    Non, I havn't got any at hands... But Google gave me that in seconds : http://www.digiphd.com/android-java-reconstruction-fast-fourier-transform-real-signal-libgdx-fft/ and I'm sure you'll found some better links than this one... – Orabîg Aug 28 '12 at 16:18
  • HI Nothing on google worked for me ... anyway Thanx for your help ... If later you found any solution to my question then please tell me... – Harsh Aug 30 '12 at 12:16
  • You keep talking about mp3Lame and mpg123. Those librairies will only help you read, encode and decode mp3 files. They **won't be of any use** regarding the core of the algorithm you are looking for. The pitch change you want requires that you do some research and have some knowledge about FFT (for example) and signal theory... – Orabîg Aug 30 '12 at 12:44
  • Nothing can "work" on Google. Google helps you find some information, but it's all about **you** to use these informations and get things working. So Google will not make things work. Your brain will. – Orabîg Aug 30 '12 at 12:45
  • I may be a little late on that one, but for the part 2, it is possible to use SoundTouch (http://www.surina.net/soundtouch/). You can compile it for android and then use a wrapper to use it inside your application. You can see an example of the wrapper within libgdx (https://code.google.com/p/libgdx/). – Al_th Jun 05 '13 at 13:24