正弦函数如何变成声音
2015-08-31 08:00
288 查看
下面有一个例子,或以帮助大家更好的理解正弦函数如何变成声音,有兴趣的朋友可以把它拷到编译器中运行。它会生成一个44100采样,单声道、16位格式的PCM文件new.pcm,可以用cooledit进行播放!
#include <math.h>
#include <iostream>
#pragma warning(disable:4996)
#pragma warning(disable:4244)
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int sample_rate =
44100;
double step =
atan(1.0)*8/sample_rate; // 2*pi/44100
int sample_scale =
(int)pow(2.0,16-1); // 2^15
FILE *pFile =
fopen("new.pcm","wb");
double t = 0.0;
double sample;
// 生成10秒数据
for(int s = 0; s <
sample_rate*10; s++)
{
sample = 0;
//
sample = (sin(t) cos(20*t) sin(30*t)*cos(t*40)
sin(t*50) cos(t*600) sin(t*125) sin(t*87))/6;
double x = min(sin(t),cos(t));
double y = max(300 200*cos(t),300
200*sin(t));
double z = max(300 200*cos(t*1.5),300
200*sin(t*1.7));
sample =
(tan(t*2)*sin(x*y*600)*abs(sin(t/10))*abs(cos(t/5)))*0.2;
sample = abs(sin(t*10))*sin(x*y)*0.45;
sample = sin(sin(z*t*0.1))*0.15;
short nSample = sample*sample_scale;
fwrite(&nSample,1,2,pFile);
fflush(pFile);
//
printf("s=%d\n",s);
t = step;
}
fclose(pFile);
return 0;
}
#include <math.h>
#include <iostream>
#pragma warning(disable:4996)
#pragma warning(disable:4244)
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int sample_rate =
44100;
double step =
atan(1.0)*8/sample_rate; // 2*pi/44100
int sample_scale =
(int)pow(2.0,16-1); // 2^15
FILE *pFile =
fopen("new.pcm","wb");
double t = 0.0;
double sample;
// 生成10秒数据
for(int s = 0; s <
sample_rate*10; s++)
{
sample = 0;
//
sample = (sin(t) cos(20*t) sin(30*t)*cos(t*40)
sin(t*50) cos(t*600) sin(t*125) sin(t*87))/6;
double x = min(sin(t),cos(t));
double y = max(300 200*cos(t),300
200*sin(t));
double z = max(300 200*cos(t*1.5),300
200*sin(t*1.7));
sample =
(tan(t*2)*sin(x*y*600)*abs(sin(t/10))*abs(cos(t/5)))*0.2;
sample = abs(sin(t*10))*sin(x*y)*0.45;
sample = sin(sin(z*t*0.1))*0.15;
short nSample = sample*sample_scale;
fwrite(&nSample,1,2,pFile);
fflush(pFile);
//
printf("s=%d\n",s);
t = step;
}
fclose(pFile);
return 0;
}
相关文章推荐
- android Matrix.setRotate 和 postRotate的区别
- [Unity5.1]同步动画UNET
- C语言的国际化
- [Windows通用应用开发] 代码编写StoryBoard时可能遇到的几个问题
- Android自学笔记之Intent 页面跳转详解
- Android自学笔记之 Android五大布局之线性布局的功能、常用属性、用法
- 第三章线程同步辅助类
- Android自学笔记之Spinner下拉框的功能、使用
- 随笔
- [LeetCode]Pow(x,n)
- Looking for an example for inserting content into the response using a servlet filter
- Java中super的几种用法并与this的区别
- Modify html response using filter
- 这两天
- Android ORM DB使用心得
- Filter that uses a response wrapper to convert all output to uppercase
- [LeetCode]Different Ways to Add Parentheses
- Spring 4 MVC HelloWorld Tutorial – Full XML Example
- 加载文档后,知道文档中有哪些“看不见的”转义字符
- EF 存储过程