博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
正弦函数如何变成声音
阅读量:7084 次
发布时间:2019-06-28

本文共 1085 字,大约阅读时间需要 3 分钟。

下面有一个例子,或以帮助大家更好的理解正弦函数如何变成声音,有兴趣的朋友可以把它拷到编译器中运行。它会生成一个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;
}
 

转载于:https://www.cnblogs.com/icoolmedia/p/sin_to_pcm.html

你可能感兴趣的文章
只有程序员才能看懂的15个瞬间
查看>>
Mybatis入门学习---使用注解开发
查看>>
影响网站关键词排名的因素
查看>>
我的友情链接
查看>>
read和变量设定方式
查看>>
Winmail + Rsync + Nmap 实现 Winmail 邮件系统双机热备
查看>>
python读写文件
查看>>
1-4常用路由协议的梳理
查看>>
Javascript核心
查看>>
利用UltraISO制作RedhatU盘启动盘
查看>>
一分钟教你快速建立起MySQL/Mariadb 主从状态检测脚本(shell)
查看>>
hive函数 -- regexp_extract
查看>>
C# vs2010 调用webservice
查看>>
Samba用户管理机制
查看>>
解决securecrt连接centos使用VIM编辑中文时乱码
查看>>
Windows server 2008R2域的安装和访问
查看>>
spring boot简单实现rest服务
查看>>
linux内核编译安装
查看>>
在CentOS/RHEL/Scientific Linux 6 & 7 上安装Telnet
查看>>
linux中hash命令:显示、添加或清除哈希表
查看>>