当前位置:网站首页>Player practice 18 xresample
Player practice 18 xresample
2022-06-12 14:09:00 【Sister Suo】
Sampling rate :
That is, the number of samples per second .
An infinite number of sampling points form the original analog signal curve . So what kind of sampling rate is more appropriate ?
Recording must ultimately be for playback . Sampling coding for storage, processing and transmission , Finally, the analog signal should be restored to play .
according to “ Nyquist sampling theory ”: When the sampled analog signal is restored , Its highest frequency is only half of the sampling frequency .
let me put it another way : To reconstruct a complete analog signal , The sampling rate should be more than twice the frequency of the analog signal .
Human auditory range :20 Hz -20 KHz. So for audio that people listen to , The sampling rate should be greater than 40 KHz When the digital signal is converted into an analog signal for playback, the sound quality will not be damaged . therefore , Appropriate sampling rate Generally greater than 40 KHz, The mainstream sampling rates are 44100HZ perhaps 48000HZ
Sampling depth ( Sampling format ):
Namely, the quantization granularity of the vertical coordinate during quantization .
In the analog signal in the figure above , The ordinate represents the loudness of the sound , Volume .
From the above figure, we can see that , When the granularity of quantification is finer , The value of the discrete point is closer to the actual value of the analog signal , If quantification , The sampling depth is 1bit, That is, quantification can only be 1 and 0, Or play the loudest sound , Or no sound .
Only the deeper the sampling depth , That is, the finer the quantization granularity , Sampled data , To get closer to the actual volume .
Now it's commonly used 16 Bit to represent the volume of a sample data .(S16)
You can see the audio format information in this media file :
format=8
audio: the sample format, the value corresponds to enum AVSampleFormat.
Corresponding table lookup :
enum AVSampleFormat {
AV_SAMPLE_FMT_NONE = -1,
AV_SAMPLE_FMT_U8, ///< unsigned 8 bits
AV_SAMPLE_FMT_S16, ///< signed 16 bits
AV_SAMPLE_FMT_S32, ///< signed 32 bits
AV_SAMPLE_FMT_FLT, ///< float
AV_SAMPLE_FMT_DBL, ///< double
AV_SAMPLE_FMT_U8P, ///< unsigned 8 bits, planar
AV_SAMPLE_FMT_S16P, ///< signed 16 bits, planar
AV_SAMPLE_FMT_S32P, ///< signed 32 bits, planar
AV_SAMPLE_FMT_FLTP, ///< float, planar
AV_SAMPLE_FMT_DBLP, ///< double, planar
AV_SAMPLE_FMT_S64, ///< signed 64 bits
AV_SAMPLE_FMT_S64P, ///< signed 64 bits, planar
AV_SAMPLE_FMT_NB
by AV_SAMPLE_FMT_S32P, ///< signed 32 bits, planar
32 Bit compact format , Because the task here is to transform it into 16 Bit for easy playback
1.xresample.h
#pragma once
struct AVCodecParameters;
struct AVFrame;
struct SwrContext;
#include <mutex>
class xresample
{
public:
// The output parameters are consistent with the input parameters except for the sampling format , Output is S16
virtual bool Open(AVCodecParameters* para);
virtual void Close();
// Returns the size after resampling , Whether successful or not, release AVFrame
virtual int Resample(AVFrame* indata, unsigned char* outdata);
xresample();
~xresample();
protected:
std::mutex mux;
SwrContext* actx = NULL;
int outformat = 1;//S16
};
The sampling rate and the number of channels remain unchanged , What you need to change is the output format , Put the output format to be changed in the header file protect in (outformat)
2.xresample.cpp
#include "xresample.h"
extern "C" {
#include <libswresample/swresample.h>
#include <libavcodec/avcodec.h>
}
#pragma comment(lib,"swresample.lib")
#include <iostream>
using namespace std;
// The output parameters are consistent with the input parameters except for the sampling format , Output is S16
bool xresample::Open(AVCodecParameters* para)
{
if (!para)return false;
mux.lock();
// Audio resampling Context initialization
//if(!actx)
// actx = swr_alloc();
// If actx by NULL Will allocate space
actx = swr_alloc_set_opts(actx,
av_get_default_channel_layout(2), // Output format
(AVSampleFormat)outformat, // Output sample format
para->sample_rate, // Output sample rate
av_get_default_channel_layout(para->channels),// Input format
(AVSampleFormat)para->format,
para->sample_rate,
0, 0
);
avcodec_parameters_free(¶);
int re = swr_init(actx);
mux.unlock();
if (re != 0)
{
char buf[1024] = {
0 };
av_strerror(re, buf, sizeof(buf) - 1);
cout << "swr_init failed! :" << buf << endl;
return false;
}
//unsigned char *pcm = NULL;
cout << "swr_int success!:" << endl;
return true;
}
void xresample::Close()
{
mux.lock();
if (actx)
swr_free(&actx);
mux.unlock();
}
xresample::xresample()
{
}
xresample::~xresample()
{
}
int xresample::Resample(AVFrame* indata, unsigned char* outdata)
{
if (!indata)return 0;
if (!outdata)
{
av_frame_free(&indata);
return 0;
}
uint8_t* data[2] = {
0 };
data[0] = outdata;
int re = swr_convert(actx, data, indata->nb_samples
, (const uint8_t**)indata->data, indata->nb_samples);
if (re <= 0)return re;
int outSize = re * indata->channels * av_get_bytes_per_sample((AVSampleFormat)outformat);
cout << "re :" << re << endl;
cout << "indata->channels:" << indata->channels << endl;
cout << "av_get_bytes_per_sample:" << av_get_bytes_per_sample((AVSampleFormat)outformat) << endl;
return outSize;
}
re by swr_convert The return value of : Number of samples per channel
return number of samples output per channel, negative value
indata->channels: Is the number of channels
av_get_bytes_per_sample: Number of bits sampled for each , That is, the format (S16) The amount of space taken up (16 Bits are two bits )
Their multiplication is the number of bits sampled for each audio channel
Use the result of this multiplication in audio playback ( Determine whether the result is larger than the remaining cache space , If the remaining cache space is large, you can play )
边栏推荐
- Communication flow analysis
- 通信流量分析
- 肝了一个月的原创小袁个人博客项目开源啦(博客基本功能都有,还包含后台管理)
- How to use Android studio to create an Alibaba cloud Internet of things app
- 简述CGI与FASTCGI区别
- Leetcode questions brushing February /1020 Number of enclaves
- 十四周作业
- Codeforces Round #798 (Div. 2)(A~D)
- Write policy of cache
- Implementation of Ackermann function with simulated recursion
猜你喜欢

Axi4 increase burst / wrap burst/ fix burst and narrow transfer

公司运营中更注重转化的出价策略,如何实现? —Google sem

肝了一个月的原创小袁个人博客项目开源啦(博客基本功能都有,还包含后台管理)

Llvm pass-- virtual function protection

Relevant knowledge points of cocoapods

Display logs in the database through loganalyzer

Summary of virtual box usage problems

The original Xiaoyuan personal blog project that has been around for a month is open source (the blog has basic functions, including background management)

Now you must know the pointer

【mysql进阶】索引分类及索引优化方案(五)
随机推荐
Leetcode 2176. 统计数组中相等且可以被整除的数对
Alibaba cloud development board haas510 sends the serial port data to the Internet of things platform
Redis core configuration and advanced data types
Pay attention to click and pursue more users to enter the website. What bidding strategy can you choose?
Cmake basic tutorial - 02 b-hello-cmake
Brush one question every day /537 Complex multiplication
如果要打造品牌知名度,可以选择什么出价策略?
Remote code injection
atomic and exclusive operation
Implementation of Ackermann function with simulated recursion
My resume.
Factory mode of "object creation" mode
Leetcode 2185. Counts the string containing the given prefix
通过loganalyzer展示数据库中的日志
Lua tvalue structure
如何使用android studio制作一个阿里云物联网APP
SystemC uses SC_ report_ Handler processing log printing
Démontage et modification de la machine publicitaire - décompression amateur
Language skills used in development
How to brush leetcode