Quick and horrible speech engine

Dependencies:   PokittoLib

Revision:
13:0333e08d59e6
Parent:
12:37d999e445ad
Child:
14:f25c2f72b2aa
--- a/main.cpp	Sat Oct 20 15:16:16 2018 +0000
+++ b/main.cpp	Sat Dec 01 13:54:35 2018 +0000
@@ -1,143 +1,190 @@
 #include "Pokitto.h"
+#include "HWSound.h"
+#include "speak.h"
+#include "music.h"
 
 Pokitto::Core mygame;
 Pokitto::Display d;
 
-mbed::DigitalOut jh_rumble(EXT0);
-mbed::DigitalIn jh_b1(EXT15);
-mbed::DigitalIn jh_b2(EXT14);
-mbed::AnalogIn jh_x(EXT1);
-mbed::AnalogIn jh_y(EXT2);
 
-#define ANALOG 256
-#define DECAY .85
-#define SEGSIZE 7
-#define number_of_segments 10
-#define MOVEANGLE .25
+typedef struct{
+    bool playSample;
+    int soundPoint;
+    const uint8_t *currentSound;
+    int currentSoundSize;
+    int volume;
+    int speed;
+} sampletype;
 
-//Sprite structure
-typedef struct
-{
-    int x,y,oldx,oldy;  //location 
-    int xSpeed, ySpeed; //speed
-    char frame; // body gfx
-}Sprite;
-Sprite marbles[number_of_segments];
+sampletype snd[10]; // up to 10 sounds at once?
+int oldQuart;
+
+Ticker sounder;
+char myWord[] = "testing Wan tu HrE for fIv           ";
 
 
-bool move_tail(void){
-    int pos_x,pos_y;
-    int bodySize = SEGSIZE*512;
-    bool collision = false;
-    for(int t=1; t<number_of_segments; t++){
-//    float rotation = atan2((float)marbles[t].y-marbles[t-1].y,(float)marbles[t].x-marbles[t-1].x);
-/*
-    // rotate the head graphic
-    if(t==1)
-    {
-        int rot = rotation*(180 / 3.14159);
-        rot+=90;
-        if(rot<0)rot+=360;
-        if(rot>360)rot-=360;
-        rot/=10;
-        rot+=3;
-//        PA_SetSpriteAnimEx(1,0,16,16,1,rot);
+void emptyBuffer(){
+    for(int t=0; t<SBUFSIZE;){
+        soundbuf[++t]=0;
     }
-*/
 
-    float x1 = marbles[t].x;
-    float y1 = marbles[t].y;
-    float x2 = marbles[t-1].x;
-    float y2 = marbles[t-1].y;
-    float rotation = atan2(y1-y2,x1-x2);
-
-    for(int s=0; s<number_of_segments; s++){
-        if(s<t-1 || s>t+1){
-            double square_difference_x = (x1 - marbles[s].x) * (x1 - marbles[s].x);
-            double square_difference_y = (y1 - marbles[s].y) * (y1 - marbles[s].y);
-            double value = sqrt(square_difference_x + square_difference_y);
-            
-            if(value < bodySize+1){
-                pos_x = x2 + bodySize * cos(rotation+MOVEANGLE);
-                pos_y = y2 + bodySize * sin(rotation+MOVEANGLE);
-
-                double square_difference_x = (pos_x - marbles[s].x) * (pos_x - marbles[s].x);
-                double square_difference_y = (pos_y - marbles[s].y) * (pos_y - marbles[s].y);
-                double value2 = sqrt(square_difference_x + square_difference_y);
-                
-                if(value < value2)rotation+=MOVEANGLE;
-                if(value > value2)rotation-=MOVEANGLE;
-                collision=true;
-            }
-        }    
+    for(int t=0; t<10; t++){
+        snd[t].playSample=0;
     }
 
-    pos_x = marbles[t-1].x + bodySize * cos(rotation);
-    pos_y = marbles[t-1].y + bodySize * sin(rotation);
-    marbles[t].x = pos_x;
-    marbles[t].y = pos_y;
+}
+
+uint8_t playSound(const unsigned char *sound, uint16_t soundSize, int volume = 255, int speed=255){
+
+    int channel=0;
+
+    snd[channel].currentSound = sound;
+    snd[channel].currentSoundSize = (soundSize*255)/speed;
+    snd[channel].soundPoint = 0;
+    snd[channel].playSample = 1;
+    snd[channel].volume = volume;
+    snd[channel].speed = speed;
+
+    return channel;
+}
+
+uint8_t mixSound(int samplePos)
+{
+    int temp = 0;
+    int t=0;
+        if(snd[t].playSample!=0){
+            int currentSample = (snd[t].currentSound[(snd[t].soundPoint*snd[t].speed)>>8]);
+            temp = currentSample;
+        }
+    return temp;
+}
 
 
-    } // for t...
-    return collision;
+void updateSample(){
+
+ int quart = soundbufindex / 512;
+ int sndOffset[]={512,1024,1536,0};
 
-}// end function
+    if(oldQuart != quart){
+        oldQuart = quart;
+        for(int t=0; t<=SBUFSIZE/4;){
+            soundbuf[t+sndOffset[quart]] = 127;
+            uint8_t sample = mixSound(t);
+            int s=0;
+            if(snd[s].playSample!=0){
+                soundbuf[t+sndOffset[quart]] = sample;
+            }
+            //for(int s=0; s<10; s++){
+                ++snd[s].soundPoint;
+                if(snd[s].soundPoint >= snd[s].currentSoundSize){
+                    snd[s].playSample=0;
+                }
+            //}
+            t++;
+        }
+    }
+}
 
 
 
 int main ()
 {
     mygame.begin();
+    pokPlayStream();
+    emptyBuffer(); // clear the sound buffer
 
-    int JCX = ((jh_x.read()*ANALOG));
-    int JCY = ((jh_y.read()*ANALOG));
+    sounder.attach(&updateSample, 0.0001);
 
+//    playSound(rawData, sizeof(rawData));
 
+    int letter=0;
     while (mygame.isRunning())
     {
         if (mygame.update())
         {
-                        
-            int xd = ((jh_x.read()*ANALOG)-JCX)*4;
-            int yd = ((jh_y.read()*ANALOG)-JCY)*4;
-            
-            // motion gravity etc.
-            marbles[0].xSpeed += -xd;
-            marbles[0].ySpeed += yd;
-            
-            // add some dampening to speed
-            marbles[0].xSpeed *= DECAY;
-            marbles[0].ySpeed *= DECAY;
-            
-            marbles[0].x += marbles[0].xSpeed;
-            marbles[0].y += marbles[0].ySpeed;
-            
-            
-            jh_rumble.write(mygame.buttons.aBtn());
-            d.color = 1;
-            d.printf("Joy X: %d\n",xd);
-            d.printf("Joy Y: %d\n",yd);
-            d.printf("B1: %d\n",jh_b1.read());
-            d.printf("B2: %d\n",jh_b2.read());
-            d.printf("A to Rumble!\n");
+            // update buttons
 
-
-            // loop until the snake no longer collides with itself.
-            while(move_tail()){}
+            if(snd[0].playSample == 0){
+                int toPlay = myWord[letter];
+                
+                if(toPlay < 97 || toPlay > 97+25){
+                    toPlay='0';
+                }
+                
+                if(toPlay>=97 && toPlay <=122){
+                    int start = sndPos[toPlay-97];
+                    int size = sndSize[toPlay-97];
+                    playSound(&ABC[start], size);
+                }
 
-            for(int t=0; t<number_of_segments; t++){
-                d.color = 0;
-                if(t==0) d.color = 1;
-                d.fillCircle(marbles[t].x>>8,marbles[t].y>>8,SEGSIZE);
-                d.color = 2;
-                d.drawCircle(marbles[t].x>>8,marbles[t].y>>8,SEGSIZE);
+                if(toPlay>=65 && toPlay <=90){
+                    int start = sndPos[toPlay-65+26];
+                    int size = sndSize[toPlay-65+26];
+                    playSound(&ABC[start], size);
+                }
+
+                if(toPlay>=48 && toPlay <=57){
+                    int start = sndPos[toPlay-48+52];
+                    int size = sndSize[toPlay-48+52];
+                    playSound(&ABC[start], size);
+                }
+
+                if(++letter==sizeof(myWord))letter=0;
             }
 
-
-
+                        
 
         }
     }
 }
 
 
+/*
+
+The sound codes are:
+A  as in bAnanA:    a
+A  as in mAp:       A
+A  as in bOther:    1
+Ay as in dAY:       2
+A  as in bAd:       3
+Aw as in nOW:       4
+Aw as in sAW:       5
+B  as in BaBy:      b
+Ch as in CHin:      c
+D  as in unDone:    d
+E  as in bEt:       e
+Ea as in bEAt:      E
+F  as in FiFty:     f
+G  as in Go:        g
+G  as in Gem:       G
+H  as in Hat:       h
+I  as in tIp:       i
+I  as in bUY:       I
+Ir as in bIRd:      j
+K  as in CooK:      k
+L  as in pooL:      l
+M  as in diM:       m
+N  as in No:        n
+Ng as in siNG:      N
+O  as in bOne:      o
+Oi as in cOIn:      O
+O  as in jOb:       6
+Oo as in wOOd:      7
+P  as in PePPer:    p
+R  as in RaRe:      r
+S  as in leSS:      s
+Sh as in SHy:       S
+T  as in aTTack:    t
+Th as in THen:      T
+Th as in THirteen:  H
+U  as in rUle:      u
+U  as in hUmdrUm:   U
+U  as in Union:     8
+U  as in cUrable:   9
+V  as in giVe:      v
+W  as in We:        w
+W  as in ONe:       W
+Z  as in raISE:     z
+Pause (about 25ms): 0
+
+*/
\ No newline at end of file