- 聲明
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);
- 把
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);
- 設(shè)置變速和變調(diào)
soundTouch->setPitch(1.0); //變調(diào)
soundTouch->setTempo(1.5);//變速
-
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)的宏定義即可
-
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));
}
- 官方示例,將一個文件變速變調(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);
}
-
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 );
}