test publish

Dependencies:   mbed GroveEarbudSensor

Files at this revision

API Documentation at this revision

Comitter:
age2pierre
Date:
Sat Mar 26 14:32:27 2016 +0000
Parent:
5:ee265ab0752d
Child:
7:d497a01156ef
Commit message:
Added MelodyGenerator class

Changed in this revision

MelodyGenerator.cpp Show annotated file Show diff for this revision Revisions of this file
MelodyGenerator.h Show annotated file Show diff for this revision Revisions of this file
Notes.h Show annotated file Show diff for this revision Revisions of this file
Scale.cpp Show annotated file Show diff for this revision Revisions of this file
Scale.h Show annotated file Show diff for this revision Revisions of this file
Speaker.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MelodyGenerator.cpp	Sat Mar 26 14:32:27 2016 +0000
@@ -0,0 +1,91 @@
+#include "MelodyGenerator.h"
+
+MelodyGenerator::MelodyGenerator()
+{
+    //J.S Bach cello suite 1 prelude
+    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|";
+    this->cursor = 0;
+}
+
+MelodyGenerator::MelodyGenerator(string myMelody)
+{
+    this->melody = myMelody;
+}
+
+vector<Notes> MelodyGenerator::getMeasure(Scale& mode)
+{
+    int length = melody.length();
+    vector<Notes> result;
+    char currentChar = melody[cursor];
+
+    bool octaveUp = false;
+    bool octaveDown = false;
+    bool flat = false;
+    bool sharp = false;
+
+    while(currentChar != '|') {
+        if (currentChar == 'u')
+            octaveUp = true;
+        else if (currentChar == 'd')
+            octaveDown = true;
+        else if (currentChar == 'b')
+            flat = true;
+        else if (currentChar == '#')
+            sharp = true;
+        else if ((currentChar == ',')||(currentChar == ';')) {
+            octaveUp = false;
+            octaveDown = false;
+            flat = false;
+            sharp = false;
+        } else {
+            Notes currentNote;
+            switch(currentChar) {
+                case '1' :
+                    currentNote = mode.getTonic();
+                    break;
+                case '2' :
+                    currentNote = mode.getSupertonic();
+                    break;
+                case '3' :
+                    currentNote = mode.getMediant();
+                    break;
+                case '4' :
+                    currentNote = mode.getSubdominant();
+                    break;
+                case '5' :
+                    currentNote = mode.getDominant();
+                    break;
+                case '6' :
+                    currentNote = mode.getSuperdominant();
+                    break;
+                case '7' :
+                    currentNote = mode.getSubtonic();
+                    break;
+            }
+
+            if (sharp)
+                currentNote = mode.applySharp(currentNote);
+            else if (flat)
+                currentNote = mode.applyFlat(currentNote);
+            if (octaveUp)
+                currentNote = mode.applyOctaveUp(currentNote);
+            else if (octaveDown)
+                currentNote = mode.applyOctaveDown(currentNote);
+
+            result.push_back(currentNote);
+        }
+    }
+
+    if (cursor == length)
+        cursor = 0;
+    else
+        cursor++;
+
+    return result;
+}
+
+void MelodyGenerator::changeMelody(string myMelody)
+{
+    this->melody = myMelody;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MelodyGenerator.h	Sat Mar 26 14:32:27 2016 +0000
@@ -0,0 +1,23 @@
+#ifndef _MELODY_GENERATOR_H_
+#define _MELODY_GENERATOR_H_
+
+#include <string>
+#include <vector>
+#include "Notes.h"
+#include "Scale.h"
+
+using namespace std;
+
+class MelodyGenerator {
+    public :
+        MelodyGenerator();
+        MelodyGenerator(string myMelody);
+        vector<Notes> getMeasure(Scale& mode);
+        void changeMelody(string myMelody);
+    protected :
+    private :
+        string melody;
+        int cursor;
+};
+
+#endif
\ No newline at end of file
--- a/Notes.h	Fri Mar 25 11:17:39 2016 +0000
+++ b/Notes.h	Sat Mar 26 14:32:27 2016 +0000
@@ -37,7 +37,8 @@
     SOL_d_6,
     LA_6,
     LA_d_6,
-    SI_6
+    SI_6,
+    SILENCE
 };
 
 #endif
--- a/Scale.cpp	Fri Mar 25 11:17:39 2016 +0000
+++ b/Scale.cpp	Sat Mar 26 14:32:27 2016 +0000
@@ -1,5 +1,35 @@
 #include "Scale.h"
 
-Scale::Scale(Notes argTonality) {
-    this->tonality = argTonality;   
+Scale::Scale(Notes argTonality)
+{
+    this->tonality = argTonality;
+}
+
+Notes Scale::applyOctaveUp(Notes myNote)
+{
+    return this->modify(12, myNote);
+}
+
+Notes Scale::applyOctaveDown(Notes myNote)
+{
+        return this->modify(-12, myNote);
+}
+
+Notes Scale::applySharp(Notes myNote)
+{
+        return this->modify(1, myNote);
+}
+
+Notes Scale::applyFlat(Notes myNote)
+{
+            return this->modify(-1, myNote);
+}
+
+Notes Scale::modify(int arg, Notes arg1)
+{
+    int rslt = (int) arg1 + arg;
+    if((arg > (int) SI_6) || (arg < (int) DO_4))
+        return arg1;
+    else
+        return (Notes) rslt;
 }
\ No newline at end of file
--- a/Scale.h	Fri Mar 25 11:17:39 2016 +0000
+++ b/Scale.h	Sat Mar 26 14:32:27 2016 +0000
@@ -15,8 +15,14 @@
         virtual Notes getDominant() = 0;
         virtual Notes getSuperdominant() = 0;
         virtual Notes getSubtonic() = 0;
+        Notes applyOctaveUp(Notes myNote);
+        Notes applyOctaveDown(Notes myNote);
+        Notes applySharp(Notes myNote);
+        Notes applyFlat(Notes myNote);
     protected:
         Notes tonality;
+    private:
+        Notes modify(int arg, Notes arg1);
 };
 
 #endif
\ No newline at end of file
--- a/Speaker.cpp	Fri Mar 25 11:17:39 2016 +0000
+++ b/Speaker.cpp	Sat Mar 26 14:32:27 2016 +0000
@@ -117,6 +117,7 @@
         case SI_6 :
             pin->period_us(253);
             break;
+        case SILENCE :
         default :
             this->mute();
             break;