Shop OBEX P1 Docs P2 Docs Learn Events
Cannot get wav_play to work in it's own COG? — Parallax Forums

Cannot get wav_play to work in it's own COG?

GeeksGoneBadGeeksGoneBad Posts: 100
edited 2013-10-06 17:31 in Propeller 1
hey guys I am trying to control a few servos, steppers, and play a wav file from the SD card - everything works except for the playing of the wav file - it will work if I don't try to run it in it's own COG but then I have to wait for all the audio to play until the motors start working and that's not good :)

any ideas? Here's my code so far.... Initially I had all of the wav playing stuff in the "PlayAudio" function but that didn't work either :/
#include "simpletools.h" 
#include "servo.h" 
#include "wavplayer.h"

void nodHead();
void moveEyes();
void turnHead();
void openCloseJaw();
void PlayAudio();
void lookLeft();
void lookRight();
void lookStraight();

unsigned int stack1[65];
unsigned int stack2[65];
unsigned int stack3[85];
unsigned int stack4[65];
const char zombie[] = {"zombie.wav"};


int main()
{
  int DO = 22, CLK = 23, DI = 24, CS = 25;
  sd_mount(DO, CLK, DI, CS); 

  cogstart(&moveEyes, NULL, stack1, sizeof(stack1));
  cogstart(&PlayAudio, NULL, stack3, sizeof(stack3));  
  cogstart(&openCloseJaw, NULL, stack2, sizeof(stack2));
  cogstart(&nodHead, NULL, stack4, sizeof(stack4));

  //pause(5000);
  //cogstart(&openCloseJaw, NULL, stack1, sizeof(stack1));
  //pause(6000);
  //servo_stop();
  pause(6000);
};

void moveEyes()
{
  lookStraight();
  servo_setramp(1, 5);
  lookLeft();  
  pause(4000);
  lookRight();
  pause(4000);
  lookStraight();
}

void nodHead()
{
  for(int a = 0; a < 5000; a++)
  {
    high(0);
    pause(1);
    low(0);
    pause(1);
  }
}

void turnHead()
{
  for(int a = 0; a < 2300; a++)
  {
    high(3);
    pause(1);
    low(3);
    pause(1);
  }
}


void lookLeft()
{
  servo_angle(1, 0);
}

void lookRight()
{
  servo_angle(1, 900);
}

void lookStraight()
{
  servo_angle(1, 450);
}

void openCloseJaw()
{
  servo_angle(2, 900);
  servo_setramp(2, 10);
  servo_angle(2, 550);
  pause(1000);
  servo_angle(2, 900);
}

void PlayAudio()
{
  wav_volume(10);
  wav_play(zombie);
  pause(3500);
  wav_stop();
}

Comments

  • ersmithersmith Posts: 6,054
    edited 2013-09-22 07:18
    I suspect you need to do the SD card access on the same COG where you sd_mount. So you could try moving the sd_mount stuff into the PlayAudio function. Or, you could play the audio in the main COG, but move it to after all the other COGs have been launched and are doing their thing.
  • GeeksGoneBadGeeksGoneBad Posts: 100
    edited 2013-09-22 09:53
    Hi and thanks for the reply! :)

    unfortunately I don't think it's completely related to the sound at all - I moved all of the SD card stuff back into the function and initially it didn't work and then I took out a few things to see what the problem might be and the sound will run in a COG but it doesn't seem to want to if I have 4 things running already - further investigation shows that I can get about three things to run and the forth does not - but what's weird is sometimes the fifth one will

    I'm going to try and combine some of the effects and hopefully I can end up with three COGs that all work :)

    I will post back success or failure! :)

    Jamie
  • GeeksGoneBadGeeksGoneBad Posts: 100
    edited 2013-09-22 15:23
    Ok - I'm getting closer! :)

    now I can get nearly everything to work by combining a few things into functions - but in my "talking" function the wav file plays the first time I call it - but not the second - the servo moves so I know it's going through the function - but the wav does not play

    close :)
    #include "simpletools.h" 
    #include "servo.h" 
    #include "wavplayer.h"
    
    void moveEyes();
    void moveHead();
    void talking();
    void biting();
    
    unsigned int stack1[75];
    unsigned int stack2[75];
    unsigned int stack3[75];
    unsigned int stack4[75];
    
    const char zombie[] = {"brains.wav"};
    int DO = 22, CLK = 23, DI = 24, CS = 25;
    
    int main()
    {
      sd_mount(DO, CLK, DI, CS); 
      cogstart(&talking, NULL, stack1, sizeof(stack1));  
    
      cogstart(&moveHead, NULL, stack2, sizeof(stack2));
      pause(100);
      cogstart(&moveEyes, NULL, stack3, sizeof(stack3));  
      pause(5000);
     
      biting();
      pause(4000);  
    
      cogstart(&talking, NULL, stack4, sizeof(stack4));  
      pause(5000);
      biting();
      pause(5000);
      biting();
    
      pause(16000);
      servo_stop();
    
    };
    
    void moveEyes()
    {
      servo_angle(12, 450);   // Look Straight
      servo_setramp(12, 10);  // Slow Look
      servo_angle(12, 0);     // Look Left
      pause(4000);
      servo_angle(12, 900);   // Look Right
      pause(4000);
      servo_angle(12, 450);   // Look Straight
      pause(4000);
      servo_setramp(12, 200); // Quick Look
      servo_angle(12, 900);   // Look Right
      pause(2000);
      servo_setramp(12, 10);  // Slow Look
      servo_angle(12, 450);   // Look Straight
      pause(2000);
      servo_setramp(12, 200); // Quick Look
      servo_angle(12, 900);   // Look Right
      pause(2000);
      servo_setramp(12, 10);  // Slow Look
      servo_angle(12, 0);     // Look Left
      pause(4000);
      servo_setramp(12, 200); // Quick Look
      servo_angle(12, 450);   // Look Straight
    }
    
    void moveHead()
    { 
      for(int a = 0; a < 760 * 1.5; a++)
      {
        high(0);
        high(1);
        pause(10);
        low(0);
        low(1);
        pause(10);
      }
    }
    
    void talking()
    {
      servo_angle(13, 900);
      servo_setramp(13, 10);
      servo_angle(13, 550); 
    
      wav_volume(10);
      wav_play(zombie);
      pause(3000);  
      wav_stop();
    
      servo_angle(13, 900);
    }
    
    void biting()
    {
      servo_angle(13, 900);
      servo_setramp(13, 20);
      servo_angle(13, 650);
      pause(300);
      servo_angle(13, 900);
      pause(300);
      servo_angle(13, 650);
      pause(300);
      servo_angle(13, 900);
      pause(300);
      servo_angle(13, 650);
      pause(300);
      servo_angle(13, 900);
    }
    
    
  • jazzedjazzed Posts: 11,803
    edited 2013-09-22 18:49
    Maybe you're out of cogs?

    The cogstart function does not attempt to stop a cog. It returns a cogid though, and you can use that to stop the previously started cog before starting a new one.
  • GeeksGoneBadGeeksGoneBad Posts: 100
    edited 2013-09-23 03:52
    Thanks Jazzed - I'll give that a try
  • GeeksGoneBadGeeksGoneBad Posts: 100
    edited 2013-09-23 03:57
    Yup that worked! thanks for all the ideas - I'm getting there! :)
  • jazzedjazzed Posts: 11,803
    edited 2013-09-23 10:21
    Yup that worked! thanks for all the ideas - I'm getting there! :)

    Great :)
  • BTL24BTL24 Posts: 54
    edited 2013-10-06 16:10
    Any more progress on the starting/stopping of cogs using wav_play? I am watching this thread with great interest.

    Any chance of posting your latest code?

    Regards,
    Brian (BTL24)
  • GeeksGoneBadGeeksGoneBad Posts: 100
    edited 2013-10-06 17:11
    Hey Brian - here's my latest code that works good for me
    #include "simpletools.h" 
    #include "servo.h" 
    #include "wavplayer.h"
    
    void moveEyes();
    void moveHead();
    void talking();
    void biting();
    
    unsigned int stack1[75];
    unsigned int stack2[75];
    unsigned int stack3[75];
    
    const char zombie[] = {"brains.wav"};
    int DO = 22, CLK = 23, DI = 24, CS = 25;
    
    int main()
    {
      sd_mount(DO, CLK, DI, CS); 
      int talkingCog = cogstart(&talking, NULL, stack1, sizeof(stack1));  
    
      cogstart(&moveHead, NULL, stack2, sizeof(stack2));
      pause(100);
      cogstart(&moveEyes, NULL, stack3, sizeof(stack3));  
      pause(5000);
     
      cogstop(talkingCog);
    
      biting();
      pause(4000);  
    
      cogstart(&talking, NULL, stack1, sizeof(stack1));  
      pause(5000);
      biting();
      pause(5000);
      biting();
    
      pause(16000);
      servo_stop();
    
    };
    
    void moveEyes()
    {
      servo_angle(12, 450);   // Look Straight
      servo_setramp(12, 10);  // Slow Look
      servo_angle(12, 0);     // Look Left
      pause(4000);
      servo_angle(12, 900);   // Look Right
      pause(4000);
      servo_angle(12, 450);   // Look Straight
      pause(4000);
      servo_setramp(12, 200); // Quick Look
      servo_angle(12, 900);   // Look Right
      pause(2000);
      servo_setramp(12, 10);  // Slow Look
      servo_angle(12, 450);   // Look Straight
      pause(2000);
      servo_setramp(12, 200); // Quick Look
      servo_angle(12, 900);   // Look Right
      pause(2000);
      servo_setramp(12, 10);  // Slow Look
      servo_angle(12, 0);     // Look Left
      pause(4000);
      servo_setramp(12, 200); // Quick Look
      servo_angle(12, 450);   // Look Straight
    }
    
    void moveHead()
    { 
      for(int a = 0; a < 760 * 1.5; a++)
      {
        high(0);
        high(10);
        pause(10);
        low(0);
        low(10);
        pause(10);
      }
    }
    
    void talking()
    {
      servo_angle(13, 900);
      servo_setramp(13, 10);
      servo_angle(13, 550); 
    
      wav_volume(10);
      wav_play(zombie);
      pause(3000);  
      wav_stop();
    
      servo_angle(13, 900);
    }
    
    void biting()
    {
      servo_angle(13, 900);
      servo_setramp(13, 20);
      servo_angle(13, 650);
      pause(300);
      servo_angle(13, 900);
      pause(300);
      servo_angle(13, 650);
      pause(300);
      servo_angle(13, 900);
      pause(300);
      servo_angle(13, 650);
      pause(300);
      servo_angle(13, 900);
    }
    
    
  • BTL24BTL24 Posts: 54
    edited 2013-10-06 17:31
    Thanks GeeksGoneBad! This really helps me as I try and resolve an issue I am having with playing wav_play function over and over again.

    I see how you stop the talking function by using talkingCog variable that lets you know which cog is playing wav file.

    I will try your structure in my code and see if I can get my wav file to play indefinitely....

    Thanks again.
    Brian (BTL24)
Sign In or Register to comment.