A very very simple orgel.

Dependencies:   mbed

orgel, a very very simple orgel program using PWM. To generate sound you need to connect a sounder(speaker?) to
D2 (PTD4) and GND.

orgel (オルゴール) PWMを使用した、とてもとても単純なオルゴール。
音を出すのには D2(PTD4) と GND にサウンダー(スピーカー?) を接続してください。

/media/uploads/Rhyme/orgel_150712.jpg

You can change tune by editing tune.h.
There you need to specify tempo,
which defines how many 4th notes per minute.
Note: each tones in tune is a 16th.

曲を変えるのには tune.h を編集してください。
最初に tempo を設定してください。
tempo は4分音符が1分間に何回かを指定します。
ノート:tune.h で指定する音はそれぞれ16分音符となります。

float tempo = 100.0 ;

Above code is saying the tempo is 100 of 4th notes per minute.

上記のコードでは♩=100 という設定になります。

Then you need to specify notes to compose the tune by
writing following code.

次に下記のコードで、曲を構成する音を記述してください。

unsigned char note[] = {
   // enter notes here as decimal values
} ;


Usable tone values are
0 for rest
1..49 for note from A3 to A7
These are index value in the note[] array.
Note: for values of each tone, please refer to tone.h and tune.h

使用可能な音の値は 
0 休符 
1..49 A3(ラ3) から A7(ラ7) 
となっています。
これらは配列 note[] のインデックス値になります。 ノート:各音の具体的な値は tone.h と tune.h を御参照ください。

As you can imagine,
note value + 1 = #
note value - 1 = ♭ 

For example
G4 = 10
G♭4 = 9
G#4 = 11

想像つかれると思いますが、インデックス値に1を足すと# 
インデックス値から1を引くと♭の音程になります。

Committer:
Rhyme
Date:
Sun Jul 12 01:36:42 2015 +0000
Revision:
1:f0f27eb31eec
Parent:
0:b1f4db5e5887
Commit before publishing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Rhyme 1:f0f27eb31eec 1 /** mbed orgel Very very simple orgel using pwm
Rhyme 1:f0f27eb31eec 2 * Copyright (c) 2015 Motoo Tanaka @ Design Methodology Lab
Rhyme 1:f0f27eb31eec 3 *
Rhyme 1:f0f27eb31eec 4 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Rhyme 1:f0f27eb31eec 5 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Rhyme 1:f0f27eb31eec 6 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Rhyme 1:f0f27eb31eec 7 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Rhyme 1:f0f27eb31eec 8 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Rhyme 1:f0f27eb31eec 9 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Rhyme 1:f0f27eb31eec 10 * THE SOFTWARE.
Rhyme 1:f0f27eb31eec 11 */
Rhyme 1:f0f27eb31eec 12
Rhyme 0:b1f4db5e5887 13 #include "mbed.h"
Rhyme 0:b1f4db5e5887 14 #include "tone.h"
Rhyme 0:b1f4db5e5887 15 #include "tune.h"
Rhyme 0:b1f4db5e5887 16
Rhyme 1:f0f27eb31eec 17 /*
Rhyme 1:f0f27eb31eec 18 * note[], num_notes, tempo are defined in tune.h
Rhyme 1:f0f27eb31eec 19 */
Rhyme 0:b1f4db5e5887 20 extern unsigned char note[] ;
Rhyme 0:b1f4db5e5887 21 extern int num_notes ;
Rhyme 1:f0f27eb31eec 22 extern float tempo ;
Rhyme 0:b1f4db5e5887 23
Rhyme 1:f0f27eb31eec 24 PwmOut out1(PTD4) ; // D2
Rhyme 0:b1f4db5e5887 25
Rhyme 0:b1f4db5e5887 26 void play_tune(void)
Rhyme 0:b1f4db5e5887 27 {
Rhyme 1:f0f27eb31eec 28 float note_length = 60.0 / (tempo * 4.0) ;
Rhyme 1:f0f27eb31eec 29
Rhyme 1:f0f27eb31eec 30 out1.write(0.0) ; // stop sound
Rhyme 0:b1f4db5e5887 31 for (int i = 0 ; i < num_notes ; i++) {
Rhyme 0:b1f4db5e5887 32 if (note[i] > 0) { // tone
Rhyme 1:f0f27eb31eec 33 out1.period_us(tone_us[note[i]]) ;
Rhyme 1:f0f27eb31eec 34 out1.write(0.5) ;
Rhyme 0:b1f4db5e5887 35 } else { // rest
Rhyme 0:b1f4db5e5887 36 out1.write(0.0) ;
Rhyme 0:b1f4db5e5887 37 }
Rhyme 0:b1f4db5e5887 38 wait(note_length) ;
Rhyme 0:b1f4db5e5887 39 }
Rhyme 0:b1f4db5e5887 40 out1.write(0.0) ;
Rhyme 0:b1f4db5e5887 41 }
Rhyme 0:b1f4db5e5887 42
Rhyme 0:b1f4db5e5887 43 int main() {
Rhyme 0:b1f4db5e5887 44 while(1) {
Rhyme 0:b1f4db5e5887 45 play_tune() ;
Rhyme 0:b1f4db5e5887 46 wait(1) ;
Rhyme 0:b1f4db5e5887 47 }
Rhyme 0:b1f4db5e5887 48 }