Example #1
0
int main(int argc, char** argv)
{
    // We catch any exceptions that might occur below -- see the catch statement for more details.
    try {
        // First, we create a Hub with our application identifier. Be sure not to use the com.example namespace when
        // publishing your application. The Hub provides access to one or more Myos.
        myo::Hub hub("com.example.hello-myo");
        std::cout << "Attempting to find a Myo..." << std::endl;

        // Next, we attempt to find a Myo to use. If a Myo is already paired in Myo Connect, this will return that Myo
        // immediately.
        // waitForAnyMyo() takes a timeout value in milliseconds. In this case we will try to find a Myo for 10 seconds, and
        // if that fails, the function will return a null pointer.
        myo::Myo* myo = hub.waitForMyo(10000);

        // If waitForAnyMyo() returned a null pointer, we failed to find a Myo, so exit with an error message.
        if (!myo) {
            throw std::runtime_error("Unable to find a Myo!");
        }
        // We've found a Myo.
        std::cout << "Connected to a Myo armband!" << std::endl << std::endl;

        // Next we construct an instance of our DeviceListener, so that we can register it with the Hub.
        DataCollector collector;

        // Hub::addListener() takes the address of any object whose class inherits from DeviceListener, and will cause
        // Hub::run() to send events to all registered device listeners.
        hub.addListener(&collector);

        //CALIBRATE FOR SIMULATED SPATIAL LOCATION
        //Copy the start position of the calibration gesture during this time.
        double roll_orient = 0;
        double yaw_orient = 0;
        for(int i=0; i<50; i++) {   //about 1 second of sampling - keep still!
            hub.run(1000/20);
            roll_orient += ROLL;
            yaw_orient += YAW;
        }
        roll_orient /= 50;
        yaw_orient /= 50;

        int hand;

        // start the sound engine with default parameters
        ISoundEngine* engine = createIrrKlangDevice();
        if (!engine)
        {
            printf("Could not startup engine\n");
            return 0; // error starting up the engine
        }
        // play some sound stream, looped

        ISound *samples[3];

        samples[0] = engine->play3D("BeatK03B 70-01.wav", vec3df(0,0,0), true, false, true);
        samples[1] = engine->play3D("GrulerK03 70B-01.wav", vec3df(0,0,0), true, false, true);
        samples[2] = engine->play3D("Wind-Mark_DiAngelo-1940285615.wav", vec3df(0,0,0), true, false, true);

        for(int i = 0; i < 3; i++) {
            samples[i]->setPosition(vec3df(0,0,1));
        }

        engine->setListenerPosition(vec3df(0,0,0), vec3df(0,0,1));

        const float radius = 1;
        int currentSample = 0;
        float vol = .5;

        while(1)
        {
            // In each iteration of our main loop, we run the Myo event loop for a set number of milliseconds.
            // In this case, we wish to update our display 20 times a second, so we run for 1000/20 milliseconds.
            hub.run(1000/20);

            // After processing events, we call the print() member function we defined above to print out the values we've
            // obtained from any events that have occurred.
            //
            // printf("Press any key to play some sound, press 'q' to quit.\n");
            // play a single sound
            collector.print();

            vec3df pos3d(radius * 2*cosf(YAW + yaw_orient - M_PI/2), 0, radius * 2*x`sinf(YAW + yaw_orient - M_PI/2));

            (WHICHARM == myo::armLeft ? hand=1 : hand=-1);
            vol = hand*(-ROLL) + roll_orient + 1;

            if (vol > 1)
                vol = 1;
            if (vol < 0)
                vol = 0;

            if (CURRENTPOSE == myo::Pose::fist) {
                if (samples[currentSample]) {
                    samples[currentSample]->setPosition(pos3d);
                }
            }
            if (CURRENTPOSE == myo::Pose::fingersSpread) {
                if (samples[currentSample]) {
                    samples[currentSample]->setVolume(vol);
                }
            }
            if (CURRENTPOSE == myo::Pose::waveIn && WAVEINCOUNTER > THRESHOLD) {
                currentSample += 1;
                currentSample %= 3;
                WAVEINCOUNTER = 1;
            }

            std::cout << '[' << "Sample: " << currentSample << "    Thresh: " << WAVEINCOUNTER << "    Vol: " << vol << "   roll/yaw orient: " << roll_orient << "  " << yaw_orient << "]";

        }
        // If a standard exception occurred, we print out its message and exit.
    } catch (const std::exception& e) {
        std::cerr << "Error: " << e.what() << std::endl;
        std::cerr << "Press enter to continue.";
        std::cin.ignore();
        return 1;
    }
}