xref: /haiku/src/tests/kits/game/simple_game_sound_test/SimpleSoundTest.cpp (revision 1a76488fc88584bf66b9751d7fb9b6527ac20d87)
1 //------------------------------------------------------------------------------
2 // SimpleSoundTest.cpp
3 //
4 // Unit test for the game kit.
5 //
6 // Copyright (c) 2001 Haiku Project
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a
9 // copy of this software and associated documentation files (the "Software"),
10 // to deal in the Software without restriction, including without limitation
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 // and/or sell copies of the Software, and to permit persons to whom the
13 // Software is furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included
16 // in all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 // DEALINGS IN THE SOFTWARE.
25 //
26 //	File Name:		SimpleSoundTest.h
27 //	Author:			Christopher ML Zumwalt May (zummy@users.sf.net)
28 //	Description:	BSimpleGameSound test application
29 //------------------------------------------------------------------------------
30 
31 #include <stdlib.h>
32 
33 #include <Button.h>
34 #include <Slider.h>
35 #include <CheckBox.h>
36 #include <FilePanel.h>
37 #include <TextControl.h>
38 
39 #include "SimpleGameSound.h"
40 #include "SimpleSoundTest.h"
41 
42 const int32 SLIDER_PAN = 'SPan';
43 const int32 SLIDER_GAIN = 'Gain';
44 const int32 BUTTON_START = 'Strt';
45 const int32 BUTTON_STOP = 'Stop';
46 const int32 BUTTON_BROWSE = 'Open';
47 const int32 CHECK_LOOP = 'Loop';
48 
49 int main(int, char**)
50 {
51 	SimpleSoundApp app("application/x-vnd.OBOS.GameKitApp");
52 	app.Run();
53 	return 0;
54 }
55 
56 
57 // SimpleSoundApp ---------------------------------------------------------
58 SimpleSoundApp::SimpleSoundApp(const char *signature)
59 	: BApplication(signature)
60 {
61 }
62 
63 
64 void SimpleSoundApp::ReadyToRun()
65 {
66 	fWin = new SimpleSoundWin(BRect(100, 200, 330, 450), "GameKit Unit Test");
67 	fWin->Show();
68 }
69 
70 
71 void SimpleSoundApp::RefsReceived(BMessage* msg)
72 {
73 	uint32 type;
74 	int32 count;
75 	msg->GetInfo("refs", &type, &count);
76 	if (type == B_REF_TYPE)
77 	{
78 		// Load the files
79 		for(int32 i = 0; i < count; i++)
80 		{
81 			entry_ref file;
82 			if (msg->FindRef("refs", i, &file) == B_OK)
83 			{
84 				BSimpleGameSound * sound = new BSimpleGameSound(&file);
85 
86 				if (sound->InitCheck() == B_OK)
87 					fWin->SetSound(sound);
88 				else
89 					delete sound;
90 			}
91 		}
92 	}
93 }
94 
95 
96 // SimpleSoundWin ---------------------------------------------------------------
97 SimpleSoundWin::SimpleSoundWin(BRect frame, const char * title)
98 	:	BWindow(frame, title, B_TITLED_WINDOW, B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS),
99 		fSound(NULL),
100 		fPanel(NULL)
101 {
102 	BRect r(10, 10, 100, 40);
103 	fBrowse = new BButton(r, "buttonBrowse", "Browse", new BMessage(BUTTON_BROWSE));
104 	fBrowse->SetEnabled(true);
105 	AddChild(fBrowse);
106 
107 	// Add the button which start and stop playback of the choosen sound
108 	r.OffsetBy(0, 50);
109 	fStart = new BButton(r, "buttonStart", "Start", new BMessage(BUTTON_START));
110 	fStart->SetEnabled(false);
111 	AddChild(fStart);
112 
113 	r.OffsetBy(110, 0);
114 	fStop = new BButton(r, "buttonStop", "Stop", new BMessage(BUTTON_STOP));
115 	fStop->SetEnabled(false);
116 	AddChild(fStop);
117 
118 	// Control the sound's volumn (gain)
119 	r.Set(10, 100, 200, 40);
120 	fGain = new BSlider(r, "sliderGain", "Gain", new BMessage(SLIDER_GAIN), 0, 100);
121 	fGain->SetHashMarks(B_HASH_MARKS_BOTTOM);
122 	fGain->SetHashMarkCount(10);
123 	fGain->SetLimitLabels("Mute", "Full");
124 	fGain->SetPosition(1.0);
125 	fGain->SetEnabled(false);
126 	AddChild(fGain);
127 
128 	// Control the sound's pan
129 	r.OffsetBy(0, 60);
130 	fPan = new BSlider(r, "sliderPan", "Pan", new BMessage(SLIDER_PAN), -100, 100);
131 	fPan->SetHashMarks(B_HASH_MARKS_BOTTOM);
132 	fPan->SetHashMarkCount(10);
133 	fPan->SetLimitLabels("Left", "Right");
134 	fPan->SetEnabled(false);
135 	AddChild(fPan);
136 
137 	r.Set(10, 220, 110, 40);
138 	fRamp = new BTextControl(r, "txtRamp", "Ramp", "20", NULL);
139 	fRamp->SetDivider(30);
140 	fRamp->SetEnabled(true);
141 	AddChild(fRamp);
142 
143 	r.Set(120, 220, 200, 40);
144 	fLoop = new BCheckBox(r, "chkLoop", "Loop", new BMessage(CHECK_LOOP));
145 	fLoop->SetEnabled(false);
146 	AddChild(fLoop);
147 }
148 
149 
150 SimpleSoundWin::~SimpleSoundWin()
151 {
152 	delete fPanel;
153 	delete fSound;
154 }
155 
156 
157 void SimpleSoundWin::Quit()
158 {
159 	be_app->PostMessage(B_QUIT_REQUESTED);
160 	BWindow::Quit();
161 }
162 
163 
164 void SimpleSoundWin::MessageReceived(BMessage *msg)
165 {
166 	int32 pos;
167 	bigtime_t ramp = bigtime_t(atoi(fRamp->Text()) * 100000.0);
168 
169 	switch (msg->what)
170 	{
171 	case BUTTON_START:
172 		fStop->SetEnabled(true);
173 		fGain->SetEnabled(true);
174 		fPan->SetEnabled(true);
175 		fLoop->SetEnabled(true);
176 
177 		fSound->StartPlaying();
178 		break;
179 
180 	case BUTTON_STOP:
181 		fStop->SetEnabled(false);
182 		fGain->SetEnabled(false);
183 		fPan->SetEnabled(false);
184 		fLoop->SetEnabled(false);
185 
186 		fSound->StopPlaying();
187 		break;
188 
189 	case SLIDER_GAIN:
190 		fSound->SetGain(fGain->Position(), ramp);
191 		break;
192 
193 	case SLIDER_PAN:
194 		pos = fPan->Value();
195 		fSound->SetPan(pos / 100.0, ramp);
196 		break;
197 
198 	case BUTTON_BROWSE:
199 		if (!fPanel) fPanel = new BFilePanel(B_OPEN_PANEL, &be_app_messenger);
200 
201 		fPanel->Show();
202 		break;
203 
204 	case CHECK_LOOP:
205 		fSound->SetIsLooping(fLoop->Value() == B_CONTROL_ON);
206 		break;
207 
208 	default:
209 		BWindow::MessageReceived(msg);
210 		break;
211 	}
212 }
213 
214 
215 void SimpleSoundWin::SetSound(BSimpleGameSound * sound)
216 {
217 	delete fSound;
218 
219 	// enable the Start button if we were given a valid sound
220 	Lock();
221 
222 	fStart->SetEnabled((sound != NULL));
223 	fLoop->SetValue(B_CONTROL_OFF);
224 	fGain->SetPosition(1.0);
225 	fPan->SetPosition(0.0);
226 
227 	Unlock();
228 
229 	fSound = sound;
230 }
231