File: soundbuffer.cpp

package info (click to toggle)
clanlib 0.5.4-1-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 10,320 kB
  • ctags: 10,893
  • sloc: cpp: 76,056; xml: 3,281; sh: 2,961; perl: 1,204; asm: 837; makefile: 775
file content (191 lines) | stat: -rw-r--r-- 4,114 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
/*
	Example of playing a soundbuffer from a wave file.
*/

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

class SoundbufferApp : public CL_ClanApplication
{
public:
	virtual char *get_title() { return "Soundbuffer application"; }

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

		try
		{
			CL_SetupCore::init();	
			CL_SetupSound::init();

			// Play some sounds
			play_sound1();
			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();

			CL_SetupSound::deinit();
			CL_SetupCore::deinit();
		}
		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(new CL_Sample("beep.wav", NULL), 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();
			CL_System::sleep(100);
		}
	}

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

		// Load sample, play it once and unload it when done:
		CL_SoundBuffer soundbuffer(new CL_Sample("beep.wav", NULL), true);
		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.scr", false);
		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_sound3_ex(bool from_datafile)
	{
		std::cout << "play_sound3_ex..." << std::endl;

		// Compile datafile if to be used:
		std::string file = "sound.scr";
		if (from_datafile)
		{
			CL_DatafileCompiler::write(file, "sound.dat");
			file = "sound.dat";
		}

		// This time load from a resource file.
		CL_ResourceManager resources(file, from_datafile);

		// 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(new CL_Streamed_WaveSample("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();
			CL_System::sleep(100);
		}
	}

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

		// Reference counting test:
		CL_ResourceManager resources("sound.scr", false);

		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);
			}
		}
	}
} app;