使用SoudTouch實現(xiàn)變速變調(diào)

  1. 聲明SoundTouch對象和內(nèi)存變量,根據(jù)聲道數(shù)和采樣率初始化對象和內(nèi)存
    SoundTouch *soundTouch = NULL;
    SAMPLETYPE *sampleBuffer = NULL;
    //采樣率
    int sample_rate=44100;
    //聲道數(shù)
    int channels =2;
    //變調(diào)
    float pitch= 1.0f;
    //變數(shù)
    float speed= 1.0f;
    //采樣位數(shù) SoudTouch最低支持16bit,所以使用16bit的來播放
    int bits= 16;
     //每秒理論PCM大小
    int BUFF_SIZE =sample_rate * channels * bits/8;
    
   
    sampleBuffer = static_cast<SAMPLETYPE *>(malloc(BUFF_SIZE));
    soundTouch = new SoundTouch();
    soundTouch->setSampleRate(sample_rate);
    soundTouch->setChannels(channels);
    soundTouch->setPitch(pitch);
    soundTouch->setTempo(speed);


  1. PCM數(shù)據(jù)給SoundTouch處理
    //采樣個數(shù),具體怎么獲取看具體情況
    int nb=0;
    //示例1 :文件讀取
    int size = fread();
    nb = size/channels;
    //示例2 :ffmpeg解碼
    int nb = swr_convert();
    
    //最大采樣數(shù)
   
    int maxSamples = BUFF_SIZE / channels; 
    
    //處理數(shù)據(jù)
    soundTouch->putSamples(sampleBuffer, nb);
    //得到數(shù)據(jù)到sampleBuffer
    int num = soundTouch->receiveSamples(sampleBuffer, maxSamples);

  1. 設(shè)置變速和變調(diào)
    soundTouch->setPitch(1.0); //變調(diào)
    soundTouch->setTempo(1.5);//變速

  1. SoudTouch選擇處理數(shù)據(jù)是16bit還是32bit,在STTypes.h里面找到
 #if !(SOUNDTOUCH_INTEGER_SAMPLES || SOUNDTOUCH_FLOAT_SAMPLES)
       
        /// Choose either 32bit floating point or 16bit integer sampletype
        /// by choosing one of the following defines, unless this selection 
        /// has already been done in some other file.
        ////
        /// Notes:
        /// - In Windows environment, choose the sample format with the
        ///   following defines.
        /// - In GNU environment, the floating point samples are used by 
        ///   default, but integer samples can be chosen by giving the 
        ///   following switch to the configure script:
        ///       ./configure --enable-integer-samples
        ///   However, if you still prefer to select the sample format here 
        ///   also in GNU environment, then please #undef the INTEGER_SAMPLE
        ///   and FLOAT_SAMPLE defines first as in comments above.
        //#define SOUNDTOUCH_INTEGER_SAMPLES     1    //< 16bit integer samples
        #define SOUNDTOUCH_FLOAT_SAMPLES       1    //< 32bit float samples
     
    #endif

根據(jù)你的類型注釋選擇對應(yīng)的宏定義即可

  1. ffmpeg里面使用的時候需要注意的點:因為FFmpeg解碼出來的PCM數(shù)據(jù)是8bit (uint8)的,而SoundTouch中最低
    16bit( 16bit integer samples),所以我們需要將8bit的數(shù)據(jù)轉(zhuǎn)換成16bit
    后再給SoundTouch處理。

8bit->16bit處理方式:

SAMPLETYPE *sampleBuffer=NULL ;
uint8_t *out_buffer = NULL;

//....初始化等

//獲取音頻數(shù)據(jù)到out_buffer
int data_size = resampleAudio(reinterpret_cast<void **>(&out_buffer));
for(int i = 0; i < data_size / 2 + 1; i++)
{
    sampleBuffer[i] = (buffer[i * 2] | ((buffer[i * 2 + 1]) << 8));
}

  1. 官方示例,將一個文件變速變調(diào)轉(zhuǎn)為另外一個文件
static void
_processFile(SoundTouch *pSoundTouch, const float pitch, const float tempo, const char *inFileName,
             const char *outFileName) {

    SoundTouch *pSoundTouch = new SoundTouch();
    
    //設(shè)置音調(diào)
    pSoundTouch->setPitch(pitch);
    //設(shè)置音速
    pSoundTouch->setTempo(tempo);
    
    int nSamples;//采樣率
    int nChannels;//聲道
    int buffSizeSamples;//每一次緩沖大小
    SAMPLETYPE sampleBuffer[BUFF_SIZE];//緩沖

    // open input file
    WavInFile inFile(inFileName);
    int sampleRate = inFile.getSampleRate();
    int bits = inFile.getNumBits();
    nChannels = inFile.getNumChannels();

    // create output file
    WavOutFile outFile(outFileName, sampleRate, bits, nChannels);

    pSoundTouch->setSampleRate(sampleRate);
    pSoundTouch->setChannels(nChannels);

    assert(nChannels > 0);
    buffSizeSamples = BUFF_SIZE / nChannels;

    // Process samples read from the input file
    while (inFile.eof() == 0) {
        int num;

        // Read a chunk of samples from the input file
        num = inFile.read(sampleBuffer, BUFF_SIZE);
        nSamples = num / nChannels;

        // Feed the samples into SoundTouch processor
        pSoundTouch->putSamples(sampleBuffer, nSamples);

        // Read ready samples from SoundTouch processor & write them output file.
        // NOTES:
        // - 'receiveSamples' doesn't necessarily return any samples at all
        //   during some rounds!
        // - On the other hand, during some round 'receiveSamples' may have more
        //   ready samples than would fit into 'sampleBuffer', and for this reason
        //   the 'receiveSamples' call is iterated for as many times as it
        //   outputs samples.
        do {
            nSamples = pSoundTouch->receiveSamples(sampleBuffer, buffSizeSamples);
            outFile.write(sampleBuffer, nSamples * nChannels);
        } while (nSamples != 0);
    }

    // Now the input file is processed, yet 'flush' few last samples that are
    // hiding in the SoundTouch's internal processing pipeline.
    pSoundTouch->flush();
    do {
        nSamples = pSoundTouch->receiveSamples(sampleBuffer, buffSizeSamples);
        outFile.write(sampleBuffer, nSamples * nChannels);
    } while (nSamples != 0);

    delete (pSoundTouch);
}

  1. ffmpeg示例

    SoundTouch *soundTouch = NULL;
    SAMPLETYPE *sampleBuffer = NULL;
    //采樣率
    int sample_rate=44100;
    //聲道數(shù)
    int channels =2;
    //變調(diào)
    float pitch= 1.0f;
    //變數(shù)
    float speed= 1.0f;
    //采樣位數(shù) SoudTouch最低支持16bit,所以使用16bit的來播放
    int bits= 16;
     //每秒理論PCM大小
    int BUFF_SIZE =sample_rate * channels * bits/8;
    
   
    sampleBuffer = static_cast<SAMPLETYPE *>(malloc(BUFF_SIZE));
    soundTouch = new SoundTouch();
    soundTouch->setSampleRate(sample_rate);
    soundTouch->setChannels(channels);
    soundTouch->setPitch(pitch);
    soundTouch->setTempo(speed);


    //獲取SoundTouch處理的數(shù)據(jù)
    int WlAudio::getSoundTouchData() {
        int maxSamples = BUFF_SIZE / channels;
        while (playstatus != NULL && !playstatus->exit) {
            out_buffer = NULL;
            if (finished) {
                finished = false;
                //獲取pcm數(shù)據(jù)到out_buffer
                data_size = resampleAudio(reinterpret_cast<void **>(&out_buffer));
                if (data_size > 0) {
                    for (int i = 0; i < data_size / 2 + 1; i++) {
                        //解碼的數(shù)據(jù)是8bit的
                        //8bit->16bit
                        sampleBuffer[i] = (out_buffer[i * 2] | ((out_buffer[i * 2 + 1]) << 8));
                    }
                    //nb 表示采樣個數(shù)  在resampleAudio里面解碼的時候通過swr_convert返回值回去
                    soundTouch->putSamples(sampleBuffer, nb);
                    //獲取處理后的數(shù)據(jù)  到sampleBuffer
                    num = soundTouch->receiveSamples(sampleBuffer,maxSamples);
                } else {
                    soundTouch->flush();
                }
            }
            if (num == 0) {
                finished = true;
                continue;
            } else {
                if (out_buffer == NULL) {
                    num = soundTouch->receiveSamples(sampleBuffer, maxSamples);
                    if (num == 0) {
                        finished = true;
                        continue;
                    }
                }
                return num;
            }
        }
        return 0;
    }
    
    //OpenSLES播放數(shù)據(jù)
    
    int buffersize = wlAudio->getSoundTouchData();
    if (buffersize > 0) {
    //兩個8bit->一個16bit     轉(zhuǎn)換為char*是為了都轉(zhuǎn)換成字節(jié)來處理
     (*wlAudio->pcmBufferQueue)->Enqueue(wlAudio->pcmBufferQueue,
                                                    (char *) wlAudio->sampleBuffer, buffersize * nChannels * 2 );
     }


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容