[Video and Audio Data Processing] 將PCM16LE雙聲道音頻採樣數據中左聲道的音量降一半

0. 代碼如下

extern "C"
{
#ifdef __cplusplus
#define __STDC_CONSTANT_MACROS

#endif

}
extern "C" {

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
}




/**
 * Halve volume of Left channel of 16LE PCM file
 * @param url  Location of PCM file.
 */
int simplest_pcm16le_halfvolumeleft(const char* url) {
	FILE* fp = fopen(url, "rb+");
	FILE* fp1 = fopen("output_halfleft.pcm", "wb+");

	int cnt = 0;

	unsigned char* sample = (unsigned char*)malloc(4);
	//申請四個字節的buffer,指向首地址

	while (!feof(fp)) {  //檢測文件結束符
		short* samplenum = NULL;
		fread(sample, 1, 4, fp); //每次一個字節一個字節的存到sample裏面

		samplenum = (short*)sample;
		//這裏samplenum就是sample的地址,別考慮啥大小端,short *就是表明取低八位數據
		//short佔兩個字節,即取低位左聲道的數據
		*samplenum = *samplenum / 2;//這裏就是裏面的值除2
		//L
		fwrite(sample, 1, 2, fp1);
		//R
		fwrite(sample + 2, 1, 2, fp1);

		cnt++;
	}
	printf("Sample Cnt:%d\n", cnt);

	free(sample);
	fclose(fp);
	fclose(fp1);
	return 0;
}





int main()
{
	simplest_pcm16le_halfvolumeleft("NocturneNo2inEflat_44.1k_s16le.pcm");
	return 0;
}

音源鏈接: https://github.com/leixiaohua1020/simplest_mediadata_test

1. 運行效果

原始聲音(肖邦-夜曲):
在這裏插入圖片描述

處理之後的音頻文件:

在這裏插入圖片描述

2. 拓展(如何單獨把右聲道音量降一半?)

其實賊簡單,就改一行代碼即可:

extern "C"
{
#ifdef __cplusplus
#define __STDC_CONSTANT_MACROS

#endif

}
extern "C" {

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
}




/**
 * Halve volume of Left channel of 16LE PCM file
 * @param url  Location of PCM file.
 */
int simplest_pcm16le_halfvolumeleft(const char* url) {
	FILE* fp = fopen(url, "rb+");
	FILE* fp1 = fopen("output_halfright.pcm", "wb+");

	int cnt = 0;

	unsigned char* sample = (unsigned char*)malloc(4);
	//申請四個字節的buffer,指向首地址

	while (!feof(fp)) {  //檢測文件結束符
		short* samplenum = NULL;
		fread(sample, 1, 4, fp); //每次一個字節一個字節的存到sample裏面

		samplenum = (short*)(sample+2);//取高位數據
		//取以sample+2地址之後short長度的數據,恰好爲高位右聲道的數據
		*samplenum = *samplenum / 2;//這裏就是裏面的值除2
		//L
		fwrite(sample, 1, 2, fp1);
		//R
		fwrite(sample + 2, 1, 2, fp1);

		cnt++;
	}
	printf("Sample Cnt:%d\n", cnt);

	free(sample);
	fclose(fp);
	fclose(fp1);
	return 0;
}





int main()
{
	simplest_pcm16le_halfvolumeleft("NocturneNo2inEflat_44.1k_s16le.pcm");
	return 0;
}

效果如下,原始波形:

在這裏插入圖片描述

處理之後的波形:
在這裏插入圖片描述

3. 再拓展(如何同時把左右聲道的數據都降低一半?)

extern "C"
{
#ifdef __cplusplus
#define __STDC_CONSTANT_MACROS

#endif

}
extern "C" {

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
}




/**
 * Halve volume of Left channel of 16LE PCM file
 * @param url  Location of PCM file.
 */
int simplest_pcm16le_halfvolumeleft(const char* url) {
	FILE* fp = fopen(url, "rb+");
	FILE* fp1 = fopen("output_halftotal.pcm", "wb+");

	int cnt = 0;

	unsigned char* sample = (unsigned char*)malloc(4);
	//申請四個字節的buffer,指向首地址

	while (!feof(fp)) {  //檢測文件結束符

		short* sampleLow = NULL;
		short* sampleHigh = NULL;

		fread(sample, 1, 4, fp); //每次一個字節一個字節的存到sample裏面

		sampleLow = (short*)(sample); //左聲道數據處理
		sampleHigh = (short*)(sample+2);//右聲道數據處理
		//取以sample+2地址之後short長度的數據,恰好爲高位右聲道的數據
		*sampleLow = *sampleLow / 2;//這裏就是裏面的值除2
		*sampleHigh = *sampleHigh / 2;
		//L
		fwrite(sample, 1, 2, fp1);
		//R
		fwrite(sample + 2, 1, 2, fp1);

		cnt++;
	}
	printf("Sample Cnt:%d\n", cnt);

	free(sample);
	fclose(fp);
	fclose(fp1);
	return 0;
}





int main()
{
	simplest_pcm16le_halfvolumeleft("NocturneNo2inEflat_44.1k_s16le.pcm");
	return 0;
}

效果如下,原始波形:

在這裏插入圖片描述
處理之後的波形如下:
在這裏插入圖片描述

參考鏈接:

  1. https://blog.csdn.net/leixiaohua1020/article/details/50534316
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章