1 #include <iostream>
2 #include <memory>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <audiere.h>
6 using namespace std;
7 using namespace audiere;
8 
9 
10 #ifdef WIN32
11 
12 #include <windows.h>
PassOut(int seconds)13 inline void PassOut(int seconds) {
14   Sleep(1000 * seconds);
15 }
16 
17 #else  // assume POSIX
18 
19 #include <unistd.h>
PassOut(int seconds)20 inline void PassOut(int seconds) {
21   sleep(seconds);
22 }
23 
24 #endif
25 
26 
27 AudioDevicePtr  g_device;
28 OutputStreamPtr g_sound;
29 OutputStreamPtr g_stream;
30 StopCallbackPtr g_callback;
31 
32 
33 class MyStopCallback : public RefImplementation<StopCallback> {
34 public:
streamStopped(StopEvent * event)35   void ADR_CALL streamStopped(StopEvent* event) {
36     cout << "Stopped!  Reason: ";
37     if (event->getReason() == StopEvent::STOP_CALLED) {
38       cout << "Stop Called";
39     } else if (event->getReason() == StopEvent::STREAM_ENDED) {
40       cout << "Stream Ended";
41     } else {
42       cout << "Unknown";
43     }
44     cout << endl;
45     if (event->getOutputStream() == g_sound) {
46       cout << "Deleting sound" << endl;
47       g_sound = 0;
48     } else if (event->getOutputStream() == g_stream) {
49       cout << "Deleting stream" << endl;
50       g_stream = 0;
51     }
52   }
53 };
54 
55 
loadStreams(bool loop=false)56 bool loadStreams(bool loop = false) {
57   g_sound  = OpenSound(g_device, "data/laugh.wav", false);
58   g_stream = OpenSound(g_device, "data/laugh.wav", true);
59   g_sound ->setRepeat(loop);
60   g_stream->setRepeat(loop);
61 
62   if (!g_sound || !g_stream) {
63     cerr << "Opening data/laugh.wav failed" << endl;
64     return false;
65   }
66   return true;
67 }
68 
69 
main(int argc,const char ** argv)70 int main(int argc, const char** argv) {
71   if (argc != 1 && argc != 2) {
72     cerr << "usage: callback [<device>]" << endl;
73     return EXIT_FAILURE;
74   }
75 
76   const char* device_name = "";
77   if (argc == 2) {
78     device_name = argv[1];
79   }
80 
81   g_device = OpenDevice(device_name);
82   if (!g_device) {
83     cerr << "OpenDevice() failed" << endl;
84     return EXIT_FAILURE;
85   }
86 
87   g_callback = new MyStopCallback;
88   g_device->registerCallback(g_callback.get());
89 
90 #define LOAD_STREAMS(x) if (!loadStreams x) { return EXIT_FAILURE; }
91 
92   LOAD_STREAMS(())
93 
94   g_sound->play();
95   g_stream->play();
96 
97   PassOut(3);
98   LOAD_STREAMS(())
99 
100   g_sound->play();
101   g_stream->play();
102   g_sound->stop();
103   g_stream->stop();
104 
105   PassOut(3);
106   LOAD_STREAMS((true))
107 
108   g_sound->play();
109   g_stream->play();
110 
111   PassOut(3);
112   LOAD_STREAMS(())
113 
114   g_sound->play();
115   g_sound->reset();
116   g_stream->play();
117   g_stream->reset();
118 
119   return EXIT_SUCCESS;
120 }
121