test publish

Dependencies:   mbed GroveEarbudSensor

Committer:
age2pierre
Date:
Thu Apr 14 14:01:28 2016 +0000
Revision:
13:879d678baf64
Parent:
8:e1beb2a9454e
Added Ticker

Who changed what in which revision?

UserRevisionLine numberNew contents of line
age2pierre 6:7cc8a333e03b 1 #include "MelodyGenerator.h"
age2pierre 6:7cc8a333e03b 2
age2pierre 6:7cc8a333e03b 3 MelodyGenerator::MelodyGenerator()
age2pierre 6:7cc8a333e03b 4 {
age2pierre 7:d497a01156ef 5 //J.S Bach cello suite 1 prelude first 7 bars
age2pierre 7:d497a01156ef 6 this->melody = "1,5,u3,u2;u3,5,u3,5;1,5,u3,u2;u3,5,u3,5|1,6,u4,u3;u4,6,u4,6;1,6,u4,u3;u4,6,u4,6|1,7,u4,u3;u4,7,u4,7;1,7,u4,u3;u4,7,u4,7|1,u1,u3,u2;u3,u1,u3,u1;1,u1,u3,u2;u3,u1,u3,u1|1,6,u3,u2;u3,u1,7,u1;6,u1,7,u1;3,5,#4,3|#4,u1,u2,u1;u2,u1,u2,u1;5,u1,u2,u1;u2,u1,u2,u1|7,u2,u5,#u4;u5,u2,u1,u2;7,u2,u1,u2;5,7,6,5|";
age2pierre 6:7cc8a333e03b 7 this->cursor = 0;
age2pierre 6:7cc8a333e03b 8 }
age2pierre 6:7cc8a333e03b 9
age2pierre 6:7cc8a333e03b 10 MelodyGenerator::MelodyGenerator(string myMelody)
age2pierre 6:7cc8a333e03b 11 {
age2pierre 6:7cc8a333e03b 12 this->melody = myMelody;
age2pierre 6:7cc8a333e03b 13 }
age2pierre 6:7cc8a333e03b 14
age2pierre 8:e1beb2a9454e 15 vector<Notes>* MelodyGenerator::getMeasure(Scale& mode)
age2pierre 6:7cc8a333e03b 16 {
age2pierre 6:7cc8a333e03b 17 int length = melody.length();
age2pierre 8:e1beb2a9454e 18 vector<Notes>* result = new vector<Notes>();
age2pierre 6:7cc8a333e03b 19 char currentChar = melody[cursor];
age2pierre 6:7cc8a333e03b 20
age2pierre 8:e1beb2a9454e 21 while(currentChar != '|') {
age2pierre 6:7cc8a333e03b 22
age2pierre 8:e1beb2a9454e 23 bool octaveUp = false;
age2pierre 8:e1beb2a9454e 24 bool octaveDown = false;
age2pierre 8:e1beb2a9454e 25 bool flat = false;
age2pierre 8:e1beb2a9454e 26 bool sharp = false;
age2pierre 8:e1beb2a9454e 27
age2pierre 6:7cc8a333e03b 28 if (currentChar == 'u')
age2pierre 6:7cc8a333e03b 29 octaveUp = true;
age2pierre 6:7cc8a333e03b 30 else if (currentChar == 'd')
age2pierre 6:7cc8a333e03b 31 octaveDown = true;
age2pierre 6:7cc8a333e03b 32 else if (currentChar == 'b')
age2pierre 6:7cc8a333e03b 33 flat = true;
age2pierre 6:7cc8a333e03b 34 else if (currentChar == '#')
age2pierre 6:7cc8a333e03b 35 sharp = true;
age2pierre 6:7cc8a333e03b 36 else if ((currentChar == ',')||(currentChar == ';')) {
age2pierre 6:7cc8a333e03b 37 octaveUp = false;
age2pierre 6:7cc8a333e03b 38 octaveDown = false;
age2pierre 6:7cc8a333e03b 39 flat = false;
age2pierre 6:7cc8a333e03b 40 sharp = false;
age2pierre 6:7cc8a333e03b 41 } else {
age2pierre 6:7cc8a333e03b 42 Notes currentNote;
age2pierre 6:7cc8a333e03b 43 switch(currentChar) {
age2pierre 6:7cc8a333e03b 44 case '1' :
age2pierre 6:7cc8a333e03b 45 currentNote = mode.getTonic();
age2pierre 6:7cc8a333e03b 46 break;
age2pierre 6:7cc8a333e03b 47 case '2' :
age2pierre 6:7cc8a333e03b 48 currentNote = mode.getSupertonic();
age2pierre 6:7cc8a333e03b 49 break;
age2pierre 6:7cc8a333e03b 50 case '3' :
age2pierre 6:7cc8a333e03b 51 currentNote = mode.getMediant();
age2pierre 6:7cc8a333e03b 52 break;
age2pierre 6:7cc8a333e03b 53 case '4' :
age2pierre 6:7cc8a333e03b 54 currentNote = mode.getSubdominant();
age2pierre 6:7cc8a333e03b 55 break;
age2pierre 6:7cc8a333e03b 56 case '5' :
age2pierre 6:7cc8a333e03b 57 currentNote = mode.getDominant();
age2pierre 6:7cc8a333e03b 58 break;
age2pierre 6:7cc8a333e03b 59 case '6' :
age2pierre 6:7cc8a333e03b 60 currentNote = mode.getSuperdominant();
age2pierre 6:7cc8a333e03b 61 break;
age2pierre 6:7cc8a333e03b 62 case '7' :
age2pierre 6:7cc8a333e03b 63 currentNote = mode.getSubtonic();
age2pierre 6:7cc8a333e03b 64 break;
age2pierre 6:7cc8a333e03b 65 }
age2pierre 6:7cc8a333e03b 66
age2pierre 6:7cc8a333e03b 67 if (sharp)
age2pierre 6:7cc8a333e03b 68 currentNote = mode.applySharp(currentNote);
age2pierre 6:7cc8a333e03b 69 else if (flat)
age2pierre 6:7cc8a333e03b 70 currentNote = mode.applyFlat(currentNote);
age2pierre 6:7cc8a333e03b 71 if (octaveUp)
age2pierre 6:7cc8a333e03b 72 currentNote = mode.applyOctaveUp(currentNote);
age2pierre 6:7cc8a333e03b 73 else if (octaveDown)
age2pierre 6:7cc8a333e03b 74 currentNote = mode.applyOctaveDown(currentNote);
age2pierre 6:7cc8a333e03b 75
age2pierre 8:e1beb2a9454e 76 result->push_back(currentNote);
age2pierre 6:7cc8a333e03b 77 }
age2pierre 8:e1beb2a9454e 78
age2pierre 8:e1beb2a9454e 79 cursor++;
age2pierre 8:e1beb2a9454e 80 if (cursor == length)
age2pierre 8:e1beb2a9454e 81 cursor = 0;
age2pierre 6:7cc8a333e03b 82
age2pierre 8:e1beb2a9454e 83 currentChar = melody[cursor];
age2pierre 8:e1beb2a9454e 84 }
age2pierre 8:e1beb2a9454e 85
age2pierre 8:e1beb2a9454e 86
age2pierre 6:7cc8a333e03b 87 cursor++;
age2pierre 8:e1beb2a9454e 88 if (cursor == length)
age2pierre 8:e1beb2a9454e 89 cursor = 0;
age2pierre 6:7cc8a333e03b 90
age2pierre 6:7cc8a333e03b 91 return result;
age2pierre 6:7cc8a333e03b 92 }
age2pierre 6:7cc8a333e03b 93
age2pierre 6:7cc8a333e03b 94 void MelodyGenerator::changeMelody(string myMelody)
age2pierre 6:7cc8a333e03b 95 {
age2pierre 6:7cc8a333e03b 96 this->melody = myMelody;
age2pierre 6:7cc8a333e03b 97 }