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