一、PCM文件格式
1、PCM文件指的是一種無損音頻文件格式,是由音頻採樣數值序列直接表示波形的一種編碼方式。
2、PCM文件有多種採樣格式,包括帶符號/無符號的8位、16位、24位和32位等。
3、PCM文件採樣率也有多種選擇,常見的包括44.1kHz和48kHz等。
4、每個採樣點的大小根據採樣格式而定,例如8位PCM每個採樣點大小為1位元組,16位PCM每個採樣點大小為2位元組。
二、PCM文件讀取
1、使用C++語言讀取PCM文件可以藉助標準庫中的文件流,例如:
std::ifstream pcmFile("audio.pcm", std::ios::binary | std::ios::in);
if (pcmFile.is_open()) {
char* buffer = new char[pcmFileSize];
pcmFile.read(buffer, pcmFileSize);
pcmFile.close();
}
2、讀取的PCM數據可以直接交給音頻處理器進行處理或保存為WAV文件等其他格式。
三、PCM文件處理
1、PCM文件的處理包括音頻剪輯、降噪、增益調整等多種功能,以下是對其中一種功能的實現:
2、音頻增量調整的實現可以通過簡單的乘法運算來實現,例如將音頻增益調整為原來的2倍:
for (int i = 0; i < pcmDataSize; i += 2) {
int16_t pcmData = buffer[i] | buffer[i + 1] << 8;
pcmData *= 2;
buffer[i] = pcmData & 0xff;
buffer[i + 1] = (pcmData >> 8) & 0xff;
}
3、以上代碼中,每個採樣點通過位運算和數據類型轉換得到16位有符號整數,再進行乘法運算調整增益,最後再將結果轉換回位元組流儲存。
四、PCM文件播放
1、使用C++語言進行PCM數據的播放可以藉助第三方庫,例如OpenAL和SDL等。
2、以下是使用SDL庫播放PCM數據的示例代碼:
SDL_AudioSpec spec;
spec.freq = 44100;
spec.format = AUDIO_S16SYS;
spec.channels = 2;
spec.samples = 1024;
spec.callback = pcmPlayerCallback;
spec.userdata = buffer;
if (SDL_OpenAudio(&spec, NULL) < 0) {
// error handling
}
SDL_PauseAudio(0);
3、以上代碼中,設置了音頻參數,並在回調函數pcmPlayerCallback
中將PCM數據寫入音頻流。
五、PCM文件轉換
1、PCM文件可以轉換為其他音頻格式,例如WAV、MP3等,常見的方法是使用FFmpeg等第三方庫。
2、以下是使用FFmpeg庫將PCM文件轉換為WAV格式的示例代碼:
AVFormatContext* fmt_ctx = NULL;
AVCodecContext* enc_ctx = NULL;
AVCodec* codec = NULL;
AVPacket pkt = { 0 };
AVFrame* frame = NULL;
int ret = 0;
// initialize demuxer and output format
ret = avformat_open_input(&fmt_ctx, pcmFileName.c_str(), NULL, NULL);
// error handling
ret = avformat_find_stream_info(fmt_ctx, NULL);
// error handling
AVOutputFormat* output_format = av_guess_format("wav", NULL, NULL);
AVFormatContext* out_fmt_ctx = NULL;
ret = avformat_alloc_output_context2(&out_fmt_ctx, output_format, NULL, NULL);
// error handling
// initialize encoder
codec = avcodec_find_encoder_by_name("pcm_s16le");
enc_ctx = avcodec_alloc_context3(codec);
// set encoder parameters
enc_ctx->bit_rate = 1411200;
enc_ctx->sample_rate = 44100;
enc_ctx->sample_fmt = AV_SAMPLE_FMT_S16;
enc_ctx->channels = 2;
// add audio stream to output format
AVStream* audio_stream = avformat_new_stream(out_fmt_ctx, NULL);
// error handling
audio_stream->id = out_fmt_ctx->nb_streams - 1;
audio_stream->time_base = { 1, enc_ctx->sample_rate };
// copy encoder parameters to output stream
avcodec_parameters_from_context(audio_stream->codecpar, enc_ctx);
// initialize encoder
ret = avcodec_open2(enc_ctx, codec, NULL);
// error handling
frame = av_frame_alloc();
// error handling
frame->format = enc_ctx->sample_fmt;
frame->nb_samples = enc_ctx->frame_size;
frame->channels = enc_ctx->channels;
ret = av_frame_get_buffer(frame, 0);
// error handling
// initialize muxer
avio_open(&out_fmt_ctx->pb, wavFileName.c_str(), AVIO_FLAG_WRITE);
// error handling
ret = avformat_write_header(out_fmt_ctx, NULL);
// error handling
// read PCM data and encode to output format
while (av_read_frame(fmt_ctx, &pkt) == 0) {
AVPacket out_pkt = { 0 };
ret = avcodec_send_packet(enc_ctx, &pkt);
// error handling
while (ret >= 0) {
ret = avcodec_receive_frame(enc_ctx, frame);
if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN)) {
break;
}
// error handling
ret = avcodec_send_frame(enc_ctx, frame);
// error handling
while (ret >= 0) {
ret = avcodec_receive_packet(enc_ctx, &out_pkt);
if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN)) {
break;
}
// error handling
av_packet_rescale_ts(&out_pkt, enc_ctx->time_base, audio_stream->time_base);
out_pkt.stream_index = audio_stream->index;
av_interleaved_write_frame(out_fmt_ctx, &out_pkt);
av_packet_unref(&out_pkt);
}
}
av_packet_unref(&pkt);
}
// write trailer
av_write_trailer(out_fmt_ctx);
// free resources
av_frame_free(&frame);
avcodec_free_context(&enc_ctx);
avformat_close_input(&fmt_ctx);
avio_close(out_fmt_ctx->pb);
avformat_free_context(out_fmt_ctx);
3、以上代碼中,使用FFmpeg庫進行音頻編碼和封裝,其中使用PCM_S16LE編碼格式進行編碼,最終將PCM文件轉換成WAV格式。
原創文章,作者:HZRKE,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/332763.html