千周声生成

 转载URL:  https://blog.csdn.net/harriszhuang/article/details/94642872

/* 这是一个简单的信号发生器,用于产生符合CSMPTE标准的千周信号 */
/* 左声道:1kHz,每间隔3秒间断约0.4秒,即出现2.6秒,静音0.4秒 */
/* 右声道:1kHz,连续 */
/* 输出格式:PCM_S16LE,16比特,低位在前,高位在后 */
/* C-Free 4.0 mingw 2.95 */

#include <math.h>
#include <stdio.h>

#define SAMPLE_RATE		48000

#define LEN				60.0

#define GATE_OFF		(0.4 * SAMPLE_RATE)
#define GATE_LOOP_LEN	(3 * SAMPLE_RATE)
#define GATE_ON			(GATE_LOOP_LEN - GATE_OFF)

#define FALSE			0
#define TRUE			1

#define INT16			1

#define GAIN_L_DB		-20
#define GAIN_R_DB		-20

short sp_to_short_word(double sp);
double dB_to_decimal(double x);

int cnt_max = (double)LEN * SAMPLE_RATE;

const char *Path = "D:\\work\\abc.pcm";
const double Pi = 3.1415926535897932384626433832795;
const double Left_freq = 1000;
const double Right_freq = 1000;
const double Left_gain = 0.1;
const double Right_gain = 0.1;

int main()
{
	int sp_cnt = 0;		/* Running on mingw2.95 */
	int gate_cnt = 0;
	int bytes_cnt = 0;
	int tmp1 = 0;
	double sp_left = 0, sp_right = 0;
	double left_freq = Left_freq, right_freq = Right_freq;
	double left_gain = dB_to_decimal(GAIN_L_DB);
	double right_gain = dB_to_decimal(GAIN_R_DB);
	double len = LEN;
	signed short sp_out[2] = { 0, 0 };

	FILE *fp1;
	fp1 = fopen(Path, "wb+");
	if (fp1 == NULL) {
		printf("Error in opening output file.\n");
		return -1;
	}

	printf("[Debug] cnt_max = %ld\n", cnt_max);
	printf("[Debug] len = %f\n", len);
	printf("[Debug] left_freq = %f\n", left_freq);
	printf("[Debug] right_freq = %f\n", right_freq);
	printf("[Debug] left_gain = %f\n", left_gain);
	printf("[Debug] right_gain = %f\n", right_gain);
	printf("[Debug] cos 30deg = %.16f\n", cos(Pi / 6));
	printf("[Debug] cos 45deg = %.16f\n", cos(Pi / 4));
	printf("[Debug] cos 60deg = %.16f\n", cos(Pi / 3));
	printf("[Debug] sizeof(signed short) = %d\n", sizeof(signed short));
	printf("[Debug] sizeof(signed int) = %d\n", sizeof(signed int));
	printf("[Debug] writing out samples...\n");

	for (sp_cnt = 0; sp_cnt < cnt_max; sp_cnt++)
	{
		double t;
		t = (double)sp_cnt / 48000;
		sp_left = cos(Pi * 2 * left_freq * t) * left_gain;
		sp_right = cos(Pi * 2 * right_freq * t) * right_gain;

		if (TRUE && gate_cnt >= GATE_ON)
			sp_left = 0;
		if (FALSE && gate_cnt >= GATE_ON)
			sp_right = 0;

#ifdef INT16
		sp_out[0] = sp_to_short_word(sp_left);
		sp_out[1] = sp_to_short_word(sp_right);
		tmp1 = fwrite(sp_out, sizeof(signed short), 2, fp1);
		bytes_cnt += tmp1;
#endif

#ifdef IEEE64
		tmp1 = fwrite(&sp_left, sizeof(double), 1, fp1);
		bytes_cnt += tmp1;
		tmp1 = fwrite(&sp_right, sizeof(double), 1, fp1);
		bytes_cnt += tmp1;
#endif

		gate_cnt = (gate_cnt >= GATE_LOOP_LEN ? 0 : gate_cnt + 1);
	} /* for */
	printf("[Debug] bytes_cnt = %ld\n", bytes_cnt);

	fclose(fp1);
} /* main */

short sp_to_short_word(double sp)
{
	signed short ret = 0;
	ret = sp * 32767;
	return ret;
}


double dB_to_decimal(double x)
{
	double y = pow(10, x / 20);
	return y;
}

/*
long sp_to_int_word(double sp)
{
signed int ret = 0;
ret = sp * 2147483647L;
}
*/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值