70歳の挑戦... 哀愁のF411CEU6で四部合唱曲演奏（その２）

　前回（その１）のようにして、PWMの周波数をタイマー別に、１ミリ秒毎に変化させることが出来ました。

 ![F411_Music.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3189926/5cd917b7-b725-7046-33ef-4d4c8bc2e3c0.jpeg)

さて、楽譜に従い必要な音の周波数と継続時間をマイコンにどのように教えるかです。次のようにしました。まず、音名とその高さの定義をします。
 |#define|音名|周波数| -|
 |:------:|:---:|:-----:|---|
 |#define|l1|440000 |ㇻの音(A)　440Hz|
 |以下同じ|...|...||
 
ここでの数字は main.c 内の「GHz 4000000000」同様、割ったときの数値が適度になるようにしてあるだけで特に深い意味はありません（4000000000/440000=9090.90）。
そして、音名とその継続時間です。このような形式はよくあるものでしょう。
 | 音名|継続時間|音の休止|　休止時間|-|
 |:----------------:|:-------:|:--------:|:---------:|-|
 |      s2         |  950     |a|50|時間はミリ秒|
 
音を出した後は少しの間、音を出さないようにします。そうしないと、音がダラダラと繋がり、いわゆるタイ(スラー)となり締まりがなくなります。このようにして、楽譜の四つの声部をコード化したのが以下です（音楽コードをプログラムコード化、なんちゃって）。

```c:onpu.h
/*
 * onpu.h
 *
 *  Created on: 2024/05/12
 *      Author: *****
 */

#ifndef INC_ONPU_H_
#define INC_ONPU_H_


#define m0  164814
#define f0  174614
#define f0s 184997
#define s0  195998
#define s0s 207652
#define l0  220000
#define l0s 233082
#define t0  246942

#define d1  261626
#define d1s 277183
#define r1  293665
#define r1s 311127
#define m1  329628
#define f1  349228
#define f1s 369994
#define s1  391995
#define s1s 415305
#define l1  440000
#define l1s 466164
#define t1  493883

#define d2  523251
#define d2s 554365
#define r2  587330
#define r2s 622254
#define m2  659255
#define f2  698456
#define f2s 739989
#define s2  783991
#define s2s 830609
#define l2  880000
#define l2s 932328
#define t2  987767

#define d3  1046502
#define d3s 1108731
#define r3  1174659
#define r3s 1244508
#define m3  1318510
#define f3  1396913
#define f3s 1479978
#define s3  1567982
#define s3s 1661219
#define l3  1760000
#define l3s 1864655
#define t3  1975533

#define a   1
#define z   0

const uint32_t sop[1000][2]=
{
	{a,1000},

	{a,4000},
	{a,4000},
	{a,4000},
	{a,4000},

	{s2,3950},{a,50},

	{s2,1950},{a,50},
	{l2,1950},{a,50},

	{s2,1950},{a,50},
	{a,1000},
	{s2,950},{a,50},

	{d3,2950},{a,50},
	{t2,950},{a,50},

	{l2,950},{a,50},
	{s2,950},{a,50},
	{l2,2950},{a,50},

	{s2,950},{a,50},
	{f2,1950},{a,50},

	{a,1000},
	{l2,950},{a,50},
	{d3,1450},{a,50},
	{t2,450},{a,50},

	{l2,950},{a,50},
	{s2,950},{a,50},
	{f2,950},{a,50},
	{m2,950},{a,50},

	{r2,3950},{a,50},

	{r2,1950},{a,50},
	{a,2000},

	{a,1000},
	{s2,950},{a,50},
	{s2,1950},{a,50},

	{m2,1950},{a,50},
	{l2,1950},{a,50},

	{f2,1950},{a,50},
	{r2,1950},{a,50},

	{s2,1950},{a,50},
	{f2,1950},{a,50},

	{m2,1950},{a,50},

	{a,2000},

	{a,4000},

	{a,1000},
	{s2,950},{a,50},
	{l2,950},{a,50},
	{d3,950},{a,50},

	{t2,950},{a,50},
	{l2,950},{a,50},
	{d3,1950},{a,50},

	{r3,1950},{a,50},
	{s2,950},{a,50},
	{s2,950},{a,50},

	{l2,950},{a,50},
	{t2,950},{a,50},
	{d3,1950},{a,50},

	{a,2000},
	{a,1000},
	{l2,950},{a,50},

	{t2,950},{a,50},
	{d3s,950},{a,50},
	{r3,1950},{a,50},

	{a,1000},
	{l2,950},{a,50},
	{l2,950},{a,50},
	{l2,950},{a,50},

	{s2,950},{a,50},
	{l2,950},{a,50},
	{l2,1950},{a,50},

	{s2,950},{a,50},
	{l2,1950},{a,50},
	{l2,950},{a,50},

	{l2,1950},{a,50},
	{l2,1950},{a,50},

	{s2,1950},{a,50},
	{r2,950},{a,50},
	{s2,1450},{a,50},

	{m2,450},{a,50},
	{l2,950},{a,50},
	{s2,1950},{a,50},

	{f2,1950},{a,50},
	{a,1000},
	{l2,950},{a,50},

	{l2,950},{a,50},
	{l2,950},{a,50},
	{s2,950},{a,50},
	{m2,950},{a,50},

	{s2,1950},{a,50},
	{d2,950},{a,50},
	{d3,1950},{a,50},

	{d3,950},{a,50},
	{d3,1950},{a,50},

	{l2,950},{a,50},
	{r3,1450},{a,50},
	{d3,250},
	{t2,250},
	{l2,950},{a,50},

	{s2,1950},{a,50},
	{a,2000},

	{a,1000},
	{d3,950},{a,50},
	{d3,950},{a,50},
	{d3,950},{a,50},

	{t2,950},{a,50},
	{d3,950},{a,50},
	{d3,1950},{a,50},

	{s2,950},{a,50},
	{d3,950},{a,50},
	{d3,950},{a,50},
	{d3,1950},{a,50},

	{t2,450},{a,50},
	{l2,450},{a,50},
	{t2,950},{a,50},
	{t2,950},{a,50},

	{l2,950},{a,50},
	{s2,1950},{a,50},
	{f2s,950},{a,50},

	{s2,950},{a,50},
	{r2,950},{a,50},
	{m2,950},{a,50},
	{s2,1450},{a,50},

	{f2,450},{a,50},
	{m2,950},{a,50},
	{r2,1950},{a,50},

	{m2,1450},{a,50},
	{f2,450},{a,50},
	{s2,450},{a,50},
	{d2,450},{a,50},

	{d3,1950},{a,50},
	{t2,950},{a,50},

	{t2,2000},

	{z,1000}

};

const uint32_t alt[1000][2]=
{
	{a,1000},

	{r3,3950},{a,50},

	{r3,1950},{a,50},
	{m3,1950},{a,50},

	{r3,1950},{a,50},
	{a,1000},
	{r3,950},{a,50},

	{s3,2950},{a,50},
	{f3,950},{a,50},

	{m3,950},{a,50},
	{r3,950},{a,50},
	{m3,1450},{a,50},
	{r3,450},{a,50},

	{m3,450},{a,50},
	{f3,450},{a,50},

	{s3,1450},{a,50},
	{f3,450},{a,50},
	{f3,1950},{a,50},
	{m3,450},{a,50},
	{r3,450},{a,50},
	{m3,1950},{a,50},

	{a,1000},
	{d3,950},{a,50},
	{f3,2950},{a,50},

	{m3,950},{a,50},
	{f3,950},{a,50},
	{f2,450},{a,50},
	{s2,450},{a,50},
	{l2,450},{a,50},
	{t2,450},{a,50},
	{d3,450},{a,50},
	{s2,450},{a,50},
	{l2,950},{a,50},
	{l2s,950},{a,50},

	{l2,450},{a,50},
	{t2,450},{a,50},
	{d3,450},{a,50},
	{r3,450},{a,50},
	{m3,950},{a,50},
	{d3,950},{a,50},

	{a,1000},
	{s2,950},{a,50},
	{d3,1450},{a,50},
	{t2,450},{a,50},

	{l2,950},{a,50},
	{s2,950},{a,50},
	{l2,1950},{a,50},

	{t2,1950},{a,50},
	{a,1000},
	{r3,950},{a,50},

	{r3,950},{a,50},
	{t2,1950},{a,50},
	{m3,1950},{a,50},
	{d3,1950},{a,50},
	{l2,1950},{a,50},
	{r3,1450},{a,50},
	{d3,450},{a,50},
	{t2,450},{a,50},
	{l2,450},{a,50},
	{s2,1450},{a,50},
	{m2,450},{a,50},
	{f2,1950},{a,50},

	{s2,950},{a,50},
	{s3,950},{a,50},
	{f3,950},{a,50},
	{m3,950},{a,50},

	{r3,950},{a,50},
	{f3,1950},{a,50},
	{m3,950},{a,50},

	{r3,1450},{a,50},
	{m3,450},{a,50},
	{f3,950},{a,50},
	{s3,950},{a,50},

	{f3,1950},{a,50},
	{a,1000},
	{s3,950},{a,50},

	{f3,950},{a,50},
	{r3,950},{a,50},
	{m3,950},{a,50},
	{s3,950},{a,50},

	{f3,950},{a,50},
	{r3,950},{a,50},
	{m3,950},{a,50},
	{l3,950},{a,50},

	{s3,950},{a,50},
	{m3,950},{a,50},
	{f3,950},{a,50},
	{m3,950},{a,50},

	{s3,950},{a,50},
	{s3,950},{a,50},
	{f3,1950},{a,50},

	{a,4000},

	{a,1000},
	{f3,950},{a,50},
	{f3,950},{a,50},
	{f3,950},{a,50},

	{m3,950},{a,50},
	{f3,950},{a,50},
	{f3,950},{a,50},
	{d3,950},{a,50},

	{f3,950},{a,50},
	{f3,950},{a,50},
	{f3,1950},{a,50},

	{r3,950},{a,50},
	{s3,1450},{a,50},
	{f3,250},
	{m3,250},
	{r3,950},{a,50},

	{m3,950},{a,50},
	{f3,1950},{a,50},
	{m3,950},{a,50},

	{a,1000},
	{f3,950},{a,50},
	{f3,950},{a,50},
	{f3,950},{a,50},

	{m3,950},{a,50},
	{d3,950},{a,50},
	{m3,1950},{a,50},

	{r3,950},{a,50},
	{m3,1950},{a,50},
	{m3,950},{a,50},

	{s3,450},{a,50},
	{f3,450},{a,50},
	{m3,450},{a,50},
	{r3,450},{a,50},
	{d3,450},{a,50},
	{t2,450},{a,50},
	{l2,1950},{a,50},
	{l2,950},{a,50},
	{r3,1950},{a,50},

	{t2,950},{a,50},
	{m3,950},{a,50},
	{m3,950},{a,50},
	{m3,950},{a,50},

	{r3,950},{a,50},
	{m3,950},{a,50},
	{m3,1950},{a,50},

	{r3,950},{a,50},
	{m3,1950},{a,50},
	{m3,950},{a,50},

	{m3,3950},{a,50},

	{r3,1950},{a,50},
	{r3,1450},{a,50},
	{m3,450},{a,50},

	{f3s,950},{a,50},
	{s3,950},{a,50},
	{l3,950},{a,50},
	{r3,950},{a,50},

	{r3,950},{a,50},
	{t2,950},{a,50},
	{d3,950},{a,50},
	{m3,1450},{a,50},

	{r3,450},{a,50},
	{d3,1950},{a,50},
	{t2,950},{a,50},

	{d3,1450},{a,50},
	{r3,450},{a,50},
	{m3,1950},{a,50},

	{r3,4000},

	{z,1000}
};
const uint32_t ten[1000][2]=
{
	{a,1000},

	{a,4000},

	{a,4000},

	{s1,3950},{a,50},

	{s1,1950},{a,50},
	{l1,1950},{a,50},

	{s1,950},{a,50},
	{s1,950},{a,50},
	{d2,2950},{a,50},

	{t1,950},{a,50},
	{l1,950},{a,50},
	{f1,950},{a,50},

	{d2,1950},{a,50},
	{l1,3950},{a,50},

	{a,2000},

	{d2,3950},{a,50},

	{d2,1950},{a,50},
	{r2,1950},{a,50},

	{d2,1950},{a,50},
	{a,1000},
	{l1,950},{a,50},

	{d2,1450},{a,50},
	{t1,450},{a,50},
	{l1,950},{a,50},
	{s1,950},{a,50},

	{f1s,950},{a,50},
	{s1,1950},{a,50},
	{f1s,950},{a,50},

	{s1,950},{a,50},
	{r2,950},{a,50},
	{r2,1950},{a,50},

	{t1,1950},{a,50},
	{m2,1950},{a,50},

	{d2,1950},{a,50},
	{l1,1950},{a,50},

	{r2,1450},{a,50},
	{d2,450},{a,50},
	{t1,450},{a,50},
	{l1,450},{a,50},
	{t1,1450},{a,50},

	{s1,450},{a,50},
	{d2,1950},{a,50},
	{t1,950},{a,50},

	{d2,950},{a,50},
	{s1,950},{a,50},
	{l1,950},{a,50},
	{d2,950},{a,50},

	{t1,950},{a,50},
	{l1,950},{a,50},
	{t1,950},{a,50},
	{d2,1950},{a,50},

	{t1,950},{a,50},
	{d2,1950},{a,50},

	{r2,950},{a,50},
	{d2,450},{a,50},
	{t1,450},{a,50},
	{l1,950},{a,50},
	{s1,950},{a,50},

	{l1,950},{a,50},
	{t1,950},{a,50},
	{d2,1950},{a,50},

	{a,2000},
	{a,1000},
	{l1,950},{a,50},

	{t1,950},{a,50},
	{d2s,950},{a,50},
	{r2,950},{a,50},
	{d2,950},{a,50},

	{r2,950},{a,50},
	{m2,950},{a,50},
	{f1,1950},{a,50},

	{d2,4000},
	{d2,4000},
	{d2,3950},{a,50},

	{r2,3950},{a,50},

	{t1,3950},{a,50},

	{d2,3950},{a,50},

	{l1,3950},{a,50},

	{d2,3950},{a,50},

	{s1,3950},{a,50},

	{m1,3950},{a,50},

	{f1s,3950},{a,50},

	{s1,11950},{a,50},

	{s1,7950},{a,50},

	{l1,3950},{a,50},

	{s1,15950},{a,50},


	{z,1000}
};

const uint32_t bas[1000][2]=
{
	{a,1000},

	{a,4000},

	{a,4000},

	{a,4000},

	{a,4000},

	{a,4000},

	{a,4000},

	{d1,3950},{a,50},

	{d1,1950},{a,50},
	{r1,1950},{a,50},

	{d1,950},{a,50},
	{d1,950},{a,50},
	{f1,2950},{a,50},

	{m1,950},{a,50},
	{r1,950},{a,50},
	{l0s,950},{a,50},

	{f1,1950},{a,50},
	{d1,950},{a,50},
	{f1,950},{a,50},

	{f1,950},{a,50},
	{m1,950},{a,50},
	{f1,950},{a,50},
	{d1,950},{a,50},

	{r1,3950},{a,50},

	{s0,1950},{a,50},
	{s1,1950},{a,50},

	{s1,1950},{a,50},
	{m1,1950},{a,50},

	{l1,1950},{a,50},
	{f1,1950},{a,50},

	{r1,1950},{a,50},
	{s1,1450},{a,50},
	{f1,450},{a,50},

	{m1,1950},{a,50},
	{r1,1950},{a,50},

	{d1,1950},{a,50},

	{a,2000},

	{a,4000},

	{s1,1950},{a,50},
	{f1,950},{a,50},
	{m1,950},{a,50},

	{r1,950},{a,50},
	{f1,1950},{a,50},
	{m1,950},{a,50},

	{r1,1950},{a,50},
	{d1,950},{a,50},
	{m1,950},{a,50},

	{f1,950},{a,50},
	{s1,950},{a,50},
	{d1,950},{a,50},
	{f1,950},{a,50},

	{s1,950},{a,50},
	{l1,950},{a,50},
	{r1,950},{a,50},
	{l1,950},{a,50},

	{s1,950},{a,50},
	{m1,950},{a,50},
	{r1,450},{a,50},
	{m1,450},{a,50},
	{f1,450},{a,50},
	{s1,450},{a,50},

	{l1,950},{a,50},
	{f1,950},{a,50},
	{f1,950},{a,50},
	{f1,950},{a,50},

	{m1,950},{a,50},
	{f1,950},{a,50},
	{f1,1950},{a,50},

	{d1,950},{a,50},
	{f1,1950},{a,50},
	{f1,950},{a,50},

	{r1,1950},{a,50},
	{r1,1950},{a,50},

	{s1,3950},{a,50},

	{d1,3950},{a,50},

	{a,1000},
	{r1,950},{a,50},
	{r1,950},{a,50},
	{r1,950},{a,50},

	{d1,950},{a,50},
	{l0,950},{a,50},
	{d1,1950},{a,50},

	{t0,950},{a,50},
	{d1,1950},{a,50},
	{d1,950},{a,50},

	{d1,1950},{a,50},
	{l0,1950},{a,50},

	{r1,3950},{a,50},

	{s0,950},{a,50},
	{d1,950},{a,50},
	{d1,950},{a,50},
	{d1,950},{a,50},

	{t0,950},{a,50},
	{d1,950},{a,50},
	{d1,1950},{a,50},

	{s0,950},{a,50},
	{d1,1950},{a,50},
	{d1,950},{a,50},

	{d1,3950},{a,50},

	{s0,3950},{a,50},

	{r1,3950},{a,50},

	{s0,1950},{a,50},
	{d1,1950},{a,50},

	{m1,1950},{a,50},
	{s1,950},{a,50},
	{s0,950},{a,50},

	{d1,3950},{a,50},

	{s0,3950},{a,50},

	{z,1000}
};

#endif /* INC_ONPU_H_ */
```
最後は {z,1000} でマイコンに終わりを知らせます。これをインクルードしてビルドします。実は、これをSDカードから読み込ませようとしたのですが、力量不足でかないませんでした。
　なお、元にした曲はルネサンス音楽の作曲家「トマス・ルイス・デ・ビクトリア(1548-1611)」の３分余りの小曲です。楽譜も出版後50年経ていますので著作権の問題はないと思います（問題ありとされるなら連絡ください）。まだ現代のような「調性」は無く、「旋法」で音楽が作られていた時代のもの。短調とも長調とも言えない微妙な哀愁漂う響きで、マイコンの演奏が胸に染みます。
　最後まで見ていただきありがとうございます。