File: testfstream.cpp

package info (click to toggle)
openalpp-cvs 20060714-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,288 kB
  • ctags: 588
  • sloc: sh: 8,763; cpp: 4,310; makefile: 172; ansic: 56
file content (208 lines) | stat: -rw-r--r-- 5,218 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
* OpenAL++ - an object oriented toolkit for spatial sound
* Copyright (C) 2002 VRlab, Ume University
*
* OpenAL++ was created using the libraries:
*                 OpenAL (http://www.openal.org), 
*              PortAudio (http://www.portaudio.com/), and
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
*
* Example of streaming from a file. Only ogg files are supported at the
* moment (2003-02-15). The program takes one argument; the name of the file
* to stream data from.
*/

#include <openalpp/alpp.h>
#include <iostream>


#define CRTDBG_MAP_ALLOC
#include <stdlib.h>
#ifdef WIN32
#include <crtdbg.h>
#endif



using namespace openalpp;

void WaitForPlaying(Source* source)
{
    while (source->getState() != openalpp::Playing)
    {
        usleep(1000);
        int checkCount = 0;
        if (checkCount++ > 1000)
        {
            throw openalpp::Error("Timed out while waiting for stream to play");
        }
    }
}

void WaitForStopped(Source* source)
{
    while (source->getState() != openalpp::Stopped)
    {
        usleep(1000);
        int checkCount = 0;
        if (checkCount++ > 1000)
        {
            throw openalpp::Error("Timed out while waiting for stream to stop");
        }
    }
}

void TestSeek()
{
    std::string numbers("one_ten_b.ogg");
    

    std::vector<float> seekPoints;
    seekPoints.push_back(7.0);
    seekPoints.push_back(5.0);
    seekPoints.push_back(2.0);
    seekPoints.push_back(3.0);

    try 
    {
        openalpp::ref_ptr<Source> source = new Source;

        source->setSound(new FileStream(numbers));
        source->setAmbient();

        for (size_t k=0; k<seekPoints.size(); ++k)
        {
            fprintf(stdout, "%.0f ", seekPoints[k]);
            source->seek(seekPoints[k]);
            if (k==0)
            {
                source->play();
            }
            usleep(500*1000);
        }
        fprintf(stdout, "\n");

        source->stop();

        WaitForStopped(source.get());

    }
    catch(openalpp::Error e) 
    {
        std::cerr << e << "\n";
    } 
    catch(...) 
    {
        std::cerr << "Unknown error!\n";
    }
    
}



int main(int argc,char **argv) {
#ifdef WIN32
    // MSVC memory leak detection
    _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); // {,,msvcr71d.dll}_crtBreakAlloc
#endif
    std::string file1("test1.ogg");
    std::string file2("test2.ogg");
    std::string file3("test3.ogg");
    
    TestSeek();
    usleep(500000);
    try 
    {

        openalpp::ref_ptr<Source> source = new Source;

        source->setSound(new FileStream(file1));
        source->setAmbient();
        source->play();

        // Test transition to audio file when first finishes
        std::cout << "Waiting for first audio file to stop" << std::endl;

        WaitForStopped(source.get());
        
        std::cout << "Playing second audio file" << std::endl;

        // For now, the source has to be re-created for stable behavior
        source = new Source; 

        source->setSound(new FileStream(file3));
        source->setAmbient();
        source->play();

        // Test pause and resume
        std::cout << "Press return to pause audio file" << std::endl;
        std::cin.get();

        source->pause();

        std::cout << "Press return to resume audio file" << std::endl;
        std::cin.get();

        source->play();

        // Test switching between files mid-stream
        std::cout << "Press return to switch to next audio file" << std::endl;
        std::cin.get();


        for (int nSwitch = 0; nSwitch < 2; nSwitch++)
        {
            source = new Source;

            source->setSound(new FileStream(file2));
            source->setAmbient();
            source->play();

            std::cout << "Press return to switch to next audio file" << std::endl;
            std::cin.get();

            source->stop();
            WaitForStopped(source.get());

            source = new Source;

            source->setSound(new FileStream(file1));
            source->setAmbient();
            source->play();

            std::cout << "Press return to switch to next audio file" << std::endl;
            std::cin.get();

            source->stop();
            WaitForStopped(source.get());

        }
        
        std::cout << "Press return to exit" << std::endl;
        std::cin.get();
    } 
    catch(openalpp::Error e) 
    {
        std::cerr << e << "\n";
    } 
    catch(...) 
    {
        std::cerr << "Unknown error!\n";
    }


    return 0;
}