This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* description: 波形情報をメモリではなく全て演算ベースにする新音源用のメモ */ | |
/* とりあえず、サイン波、矩形波、ノコギリ波のみ */ | |
/* ノイズ発生装置は後回し */ | |
#include <math.h> | |
#define PI 3.1415926535897932 | |
#define PI2 6.2831853071795864 | |
double calcwav_sin(double r) | |
{ | |
return sin(r); | |
} | |
double calcwav_square(double r) | |
{ | |
r=fmod(r,PI2); | |
if(r<0.0) r+=PI2; | |
if(r<PI) return 1.0; | |
return -1.0; | |
} | |
double calcwav_pulse(double r) | |
{ | |
r=fmod(r,PI2); | |
if(r<0.0) r+=PI2; | |
r/=PI; | |
r-=1.0; | |
return r; | |
} |
上記はその新音源の波形生成ルーチンです。
波形データをメモリに持つ仕組みではなく、関数で求める形。
サイン波、矩形波、ノコギリ波の3つ。
2πで1Hzということになります。
【音程算出】
サンプリング周波数44100Hzで音程周波数440Hz(ラ)を出す場合、
2π ÷ (44100 ÷ 440)
で、radの増分が求まるという感じです。
だいたい0.063ぐらいかな。
(そして、radのカーソルをチャネル毎に持たせる)
それを毎回計算すると処理性能が悪くなってしまうので、音程ごとの増分値をテーブルに持たせれば、性能的に良い感じになる筈です。
テーブルに持たせる増分値を求めるプログラムを作成。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 88鍵ピアノの音階周波数 & サンプリング周波数1Hzあたりのラジアン増分を求めて表示 */ | |
#include <stdio.h> | |
#define S 44100.00 | |
#define B 1.0594631 | |
#define PI 3.1415926535897932 | |
#define PI2 6.2831853071795864 | |
int main(int argc,char* argv[]) | |
{ | |
const char* t[12]={ | |
"A " | |
,"A#" | |
,"B " | |
,"C " | |
,"C#" | |
,"D " | |
,"D#" | |
,"E " | |
,"F " | |
,"F#" | |
,"G " | |
,"G#" | |
}; | |
double hz[88]; | |
int n=0; | |
double a=13.75; | |
double b; | |
for(n=0;n<88;n++) { | |
if(n%12==0) { | |
a*=2.0; | |
hz[n]=a; | |
} else { | |
hz[n] = hz[n-1] * B; | |
} | |
b=PI2/(S/hz[n]); | |
printf("%s : %9.4fHz : %.4f rad/1Hz\n",t[n%12],hz[n],b); | |
} | |
return 0; | |
} |
上記プログラムで求めたテーブルは↓のような感じ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
double ARTBL[88] = { | |
0.0039180860759056 | |
,0.0041510676200458 | |
,0.0043979029690434 | |
,0.0046594159130819 | |
,0.0049364792274631 | |
,0.0052300175854136 | |
,0.0055410106440968 | |
,0.0058704963141278 | |
,0.0062195742235045 | |
,0.0065894093875141 | |
,0.0069812360968648 | |
,0.0073963620370163 | |
,0.0078361721518113 | |
,0.0083021352400916 | |
,0.0087958059380867 | |
,0.0093188318261638 | |
,0.0098729584549261 | |
,0.0104600351708273 | |
,0.0110820212881937 | |
,0.0117409926282557 | |
,0.0124391484470089 | |
,0.0131788187750282 | |
,0.0139624721937296 | |
,0.0147927240740326 | |
,0.0156723443036225 | |
,0.0166042704801833 | |
,0.0175916118761735 | |
,0.0186376636523276 | |
,0.0197459169098523 | |
,0.0209200703416545 | |
,0.0221640425763874 | |
,0.0234819852565114 | |
,0.0248782968940178 | |
,0.0263576375500565 | |
,0.0279249443874593 | |
,0.0295854481480652 | |
,0.0313446886072451 | |
,0.0332085409603666 | |
,0.0351832237523470 | |
,0.0372753273046551 | |
,0.0394918338197046 | |
,0.0418401406833091 | |
,0.0443280851527748 | |
,0.0469639705130227 | |
,0.0497565937880356 | |
,0.0527152751001130 | |
,0.0558498887749185 | |
,0.0591708962961304 | |
,0.0626893772144902 | |
,0.0664170819207332 | |
,0.0703664475046939 | |
,0.0745506546093103 | |
,0.0789836676394092 | |
,0.0836802813666181 | |
,0.0886561703055495 | |
,0.0939279410260454 | |
,0.0995131875760713 | |
,0.1054305502002260 | |
,0.1116997775498371 | |
,0.1183417925922608 | |
,0.1253787544289804 | |
,0.1328341638414663 | |
,0.1407328950093879 | |
,0.1491013092186206 | |
,0.1579673352788184 | |
,0.1673605627332363 | |
,0.1773123406110990 | |
,0.1878558820520909 | |
,0.1990263751521426 | |
,0.2108611004004520 | |
,0.2233995550996741 | |
,0.2366835851845215 | |
,0.2507575088579608 | |
,0.2656683276829326 | |
,0.2814657900187757 | |
,0.2982026184372412 | |
,0.3159346705576367 | |
,0.3347211254664726 | |
,0.3546246812221980 | |
,0.3757117641041818 | |
,0.3980527503042852 | |
,0.4217222008009040 | |
,0.4467991101993482 | |
,0.4733671703690431 | |
,0.5015150177159216 | |
,0.5313366553658653 | |
,0.5629315800375514 | |
,0.5964052368744823 | |
}; |
【波形合成】
基本波形をベースに合成波形も出力できるようにすれば、出力できる音色数は7種類。
- sin
- square
- pulse
- sin + square
- sin + pulse
- square + pulse
- sin + square + pulse
あとはノイズ1種類 + 7種それぞれとノイズの合成で15種類
0 件のコメント:
コメントを投稿
注: コメントを投稿できるのは、このブログのメンバーだけです。