使用soundtouch开源库实现ffplay倍速播功能

本文介绍如何使用SoundTouch音频处理库为FFmpeg项目中的ffplay添加倍速播放功能,包括SoundTouch库的编译、C风格API封装及ffplay中的集成应用。


一、SoundTouch音频处理库的编译

soundtouch项目官网 https://www.surina.net/soundtouch/
SoundTouch git repository: https://codeberg.org/soundtouch/soundtouch.git

windows版本编译步骤

克隆项目代码

工具:git visiual studio 2019

// 下载源码 当前版本 2.3.1
git clone https://codeberg.org/soundtouch/soundtouch.git

修改soundtouch\include\STTypes.h下的宏定义
注意:默认是使用32bit float samples,需要更改成使用16bit integer samples

 #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
vs2019编译soundtouch静态库和动态库

打开vs命令行工具 Developer Command Prompt for VS 2019
切换到源码路径执行命令 make-win.bat
生成的静态库动态库在 lib文件夹下面
vs解决方案在: soundtouch\source\SoundTouch\SoundTouch.sln

二、封装soundtouch库变速接口

对soundtouch库进行c风格api封装,提供变速接口给ffplay调用
头文件:soundtouch_wrap.h
#ifndef IJKSOUNDTOUCHWRAP_H
#define IJKSOUNDTOUCHWRAP_H

#include <stdint.h>

void* soundtouch_create();

int soundtouch_translate(void *handle, short* data, float speed, float pitch,
						int len, int bytes_per_sample, int n_channel, int n_sampleRate);

void soundtouch_destroy(void *handle);

#endif /* IJKSOUNDTOUCHWRAP_H */
实现文件:soundtouch_wrap.cpp
#include "SoundTouch.h"

using namespace std;
using namespace soundtouch;

void* soundtouch_create() 
{
   
   
    SoundTouch *handle_ptr = new SoundTouch();
    const char *version = handle_ptr->getVersionString();
    return handle_ptr;
}

int soundtouch_translate(void *handle, short* data, float speed, float pitch,
	int len, int bytes_per_sample, int n_channel, int n_sampleRate) 
{
   
   
    SoundTouch *handle_ptr = (SoundTouch*)handle;
    int put_n_sample = len / n_channel;
    int nb = 0;
    int pcm_data_size = 0;
    if (handle_ptr == NULL)
        return 0;

    handle_ptr->setPitch(pitch);
    handle_ptr->setRate(speed);

    handle_ptr->setSampleRate(n_sampleRate);
    handle_ptr->setChannels(n_channel);

    handle_ptr->putSamples((SAMPLETYPE*)data, put_n_sample);

    do {
   
   
        nb = handle_ptr->receiveSamples((SAMPLETYPE*)data, n_sampleRate / n_channel);
        pcm_data_size += nb * n_channel * bytes_per_sample
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值