File: soundbuffer.cpp

package info (click to toggle)
clanlib 0.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 28,372 kB
  • ctags: 16,520
  • sloc: cpp: 101,145; sh: 8,752; xml: 6,410; makefile: 1,740; ansic: 463; perl: 424; php: 247
file content (267 lines) | stat: -rw-r--r-- 5,913 bytes parent folder | download | duplicates (7)
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
	Example of playing a soundbuffer from a wave file.
*/

#include <ClanLib/core.h>
#include <ClanLib/application.h>
#include <ClanLib/sound.h>
#include <ClanLib/vorbis.h>
#include <iostream>

class SoundbufferApp : public CL_ClanApplication
{
public:
	virtual int main(int, char **)
	{
		CL_ConsoleWindow console("SoundbufferApp");
		console.redirect_stdio();

		try
		{
			CL_SetupCore setup_core;
			CL_SetupSound setup_sound;

			CL_SoundOutput output(44100);

			// Test recording:
//			rec_sound();

			// Play some sounds
			play_sound1();
			play_sound2();
//			play_sound3();
			
			// Play some sounds from resource script
//			play_sound3_ex(false);
			
			// Play some sounds from datafile
//			play_sound3_ex(true);

			// Play streamed sound:
//			play_sound4();

			// Play from resource script, reference count test:
//			play_sound5();

			// Play two sounds at once
			play_sound6();

			// Play ogg file:
//			play_sound7();
		}
		catch (CL_Error err)
		{
			std::cout << "error: " << err.message.c_str() << std::endl;

			console.display_close_message();
		}

		return 0;
	}

	void play_sound1()
	{
		std::cout << "play_sound1..." << std::endl;

		// Load a sample from a wave file:
		CL_SoundBuffer soundbuffer("beep.wav");

		// Setup a session handle for the playback:
		CL_SoundBuffer_Session session = soundbuffer.prepare();

		// Start the playback:
		session.play();

		// Wait until the sample finishes:
		while(session.is_playing())
		{
			CL_System::keep_alive(100);
		}
	}

	void play_sound2()
	{
		std::cout << "play_sound2..." << std::endl;

		// Load sample, play it once and unload it when done:
		CL_SoundBuffer soundbuffer("beep.wav");
		soundbuffer.play();

		// Note: if you call this function instead of play_sound() in this
		//       example, you wont actually hear the sample because program
		//       exits right away.
	}

	void play_sound3()
	{
		std::cout << "play_sound3..." << std::endl;

		// This time load from a resource file.
		CL_ResourceManager resources("sound.xml");
		CL_SoundBuffer soundbuffer("beep", &resources);

		// Setup a session handle for the playback:
		CL_SoundBuffer_Session session = soundbuffer.prepare();

		// Start the playback:
		session.play();

		// Wait until the sample finishes:
		while(session.is_playing())
		{
			CL_System::keep_alive(100);
		}
	}

	void play_sound3_ex(bool from_datafile)
	{
		std::cout << "play_sound3_ex..." << std::endl;

		// Use a zip archive as datafile if needed:
		CL_Zip_Archive *zip_file = 0;
		bool delete_provider = false;

		if (from_datafile)
		{
			// todo: create zip archive on the fly here, or include it with example?

			zip_file = new CL_Zip_Archive("sound.zip");
			delete_provider = true;
		}

		// This time load from a resource file.
		CL_ResourceManager resources("sound.xml", zip_file, delete_provider);

		// Do a multiple resource load test:
		for (int i=0; i<3; i++)
		{
			CL_SoundBuffer soundbuffer("beep", &resources);

			// Setup a session handle for the playback:
			CL_SoundBuffer_Session session = soundbuffer.prepare();

			// Start the playback:
			session.play();

			// Wait until the sample finishes:
			while(session.is_playing())
			{
				CL_System::keep_alive();
				CL_System::sleep(100);
			}
		}
	}

	void play_sound4()
	{
		std::cout << "play_sound4..." << std::endl;

		// Load sample as streaming:
		CL_SoundBuffer soundbuffer("beep.wav", true);

		// Setup a session handle for the playback:
		CL_SoundBuffer_Session session = soundbuffer.prepare();

		// Start the playback:
		session.play();

		// Wait until the sample finishes:
		while(session.is_playing())
		{
			CL_System::keep_alive(100);
		}
	}

	void play_sound5()
	{
		std::cout << "play_sound5..." << std::endl;

		// Reference counting test:
		CL_ResourceManager resources("sound.xml");

		for (int i=0; i<5; i++)
		{
			CL_SoundBuffer soundbuffer("beep", &resources);

			// Setup a session handle for the playback:
			CL_SoundBuffer_Session session = soundbuffer.prepare();

			// Start the playback:
			session.play();

			// Wait until the sample finishes:
			while(session.is_playing())
			{
				CL_System::keep_alive();
				CL_System::sleep(100);
			}
		}
	}

	void play_sound6()
	{
		std::cout << "play_sound6..." << std::endl;

		// Load a sample from a wave file:
		CL_SoundBuffer soundbuffer1("gun.wav");
		CL_SoundBuffer soundbuffer2("explode.wav");

		// Setup a session handle for the playback:
		CL_SoundBuffer_Session session1 = soundbuffer1.prepare();
		CL_SoundBuffer_Session session2 = soundbuffer2.prepare();

		// Start the playback:
		session1.play();
		session2.play();

		// Wait until one of the sounds finishes:
		while(session1.is_playing() || session2.is_playing())
		{
			CL_System::keep_alive(100);
		}
	}

	void play_sound7()
	{
		std::cout << "play_sound7..." << std::endl;

		CL_SetupVorbis setup_vorbis;
		CL_SoundBuffer soundbuffer("KeepHopeAlive.ogg");
		CL_SoundBuffer_Session session = soundbuffer.play();
		while (session.is_playing()) CL_System::keep_alive(100);
	}

	void rec_sound()
	{
/*		std::cout << "rec_sound..." << std::endl;
		CL_StreamSoundProvider *mic = CL_Sound::get_microphone();
		if (mic == 0) return; // no mic support.

		CL_StreamSoundProvider_Session *mic_session = mic->begin_session();

		std::cout << "recording..." << std::endl;
		mic_session->play();
		CL_System::sleep(10000);
		mic_session->stop();

		// Get for 10 secs of audio data:
		unsigned char buffer[22050*4*10];
		int received = mic_session->get_data(buffer, 22050*4*10);

		mic->end_session(mic_session);

		// Put it into a raw sample and play it:
		CL_SoundBuffer sample(
			new CL_Sample_RawData(buffer, received, 2, true),
			true);

		std::cout << "playing back recording..." << std::endl;
		CL_SoundBuffer_Session session = sample.play();

		// Wait until the sample finishes:
		while(session.is_playing())
		{
			CL_System::keep_alive(100);
		} */
	}
} app;