File: streamed_mikmod_sample.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 (231 lines) | stat: -rw-r--r-- 5,637 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
	$Id: streamed_mikmod_sample.cpp,v 1.9 2001/12/11 20:44:23 mbn Exp $

	------------------------------------------------------------------------
	ClanLib, the platform independent game SDK.

	This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
	version 2. See COPYING for details.

	For a total list of contributers see CREDITS.

	------------------------------------------------------------------------

	File purpose:
		Streamed sample

*/

#include "API/MikMod/streamed_mikmod_sample.h"
#include "resourcetype_module.h"
#include "module_reader.h"



/*****************************
      CL_Streamed_MikModSample
*****************************/

CL_SoundBuffer *CL_Streamed_MikModSample::create(
	const std::string &filename,
	CL_InputSourceProvider *inputprovider, 
	bool looped)
{
	return CL_SoundBuffer::create(new CL_Streamed_MikModSample(filename, inputprovider, looped), true);
}

CL_SoundBuffer *CL_Streamed_MikModSample::load(
	const std::string &res_id,
	CL_ResourceManager *manager, 
	bool looped)
{
	return CL_SoundBuffer::create(new CL_Streamed_MikModSample(res_id, manager, looped), true);
}

CL_Streamed_MikModSample::CL_Streamed_MikModSample(
	const std::string &_filename, 
	CL_InputSourceProvider *_inputprovider, 
	bool _looped)
{
	loaded_from_resource=false;

	CL_InputSourceProvider *inputprovider;
	filename = _filename;
	looped = _looped;

	if (_inputprovider == NULL)
	{
		inputprovider = CL_InputSourceProvider::create_file_provider("");
	}
	else
	{
		inputprovider = _inputprovider->clone();
	}

	/*
	 * Here we use the same method than when loading the module
	 * from a .scr file. Still there's a difference, since here
	 * we don't use the resource mecanism and therefore will
	 * need to free the module manually later
	 */

	CL_InputSource *input=inputprovider->open_source(filename.c_str());
	cl_assert ( input != NULL );

	MREADER *reader=new_clanlib_reader((void *) input);
	cl_assert ( reader != NULL );

	module = Player_LoadGeneric(reader,CLANLIB_READER_CHANNELS,0);

	if (module == NULL)
	{
		throw CL_Error(MikMod_strerror(MikMod_errno));
	}

	delete_clanlib_reader(reader);
	delete input;
	delete inputprovider;
}

CL_Streamed_MikModSample::CL_Streamed_MikModSample(
	const std::string &_res_id, 
	CL_ResourceManager *_manager, 
	bool _looped)
{
	loaded_from_resource=true;

        manager=_manager;
	filename=_res_id;

	looped=_looped;
	CL_Resource res;
	CL_ResourceData_Module *resdata;
	res=manager->get_resource(filename);
	res.load();
	resdata=(CL_ResourceData_Module *) res.get_data("module");
	module=resdata->module;
}

CL_Streamed_MikModSample::~CL_Streamed_MikModSample()
{
	if (loaded_from_resource)
	{
		/*
		 * The sample has been loaded from a resource file therefore
		 * we call unload(), and if needed the resource manager
		 * will free the module
		 */
		CL_Resource res;
		res=manager->get_resource(filename);
		res.unload();
	}
	else
	{
		/*
		 * The sample has been loaded directly from a file,
		 * we free it right away.
		 */
		Player_Free(module);
	}
}

CL_StreamSoundProvider_Session *CL_Streamed_MikModSample::begin_session()
{
	return new CL_Streamed_MikModSample_Session(module, looped);
}

void CL_Streamed_MikModSample::end_session(CL_StreamSoundProvider_Session *session)
{
	delete session;
}


/*****************************
  CL_Streamed_MikModSample_Session
*****************************/

CL_Streamed_MikModSample_Session::CL_Streamed_MikModSample_Session(
	MODULE *_module,
	bool _looped)
{
	module=_module;
	looped=_looped;

	cl_assert (module != NULL);

	//	module->loop=looped;
	module->wrap=looped;

	Player_Start (module);

	sample_freq = md_mixfreq;

	int bytes_per_sample = ((md_mode&DMODE_STEREO)?2:1) * ((md_mode&DMODE_16BITS)?2:1);

	if (((md_mode&DMODE_STEREO)?2:1) == 2 && bytes_per_sample == 4) sample_format = sf_16bit_signed_stereo;
	else if (((md_mode&DMODE_STEREO)?2:1) == 2 && bytes_per_sample == 2) sample_format = sf_8bit_signed_stereo;
	else if (((md_mode&DMODE_STEREO)?2:1) == 1 && bytes_per_sample == 2) sample_format = sf_16bit_signed;
	else if (((md_mode&DMODE_STEREO)?2:1) == 1 && bytes_per_sample == 1) sample_format = sf_8bit_signed;
	else
	{
		std::cout << "    Invalid wave file format         " << std::endl;
		std::cout << "---------------------------------" << std::endl;
		std::cout << "Sample size: " << sample_size << std::endl;
		std::cout << "Sample frequency: " << sample_freq << std::endl;
		std::cout << "Number of channels: " << ((md_mode&DMODE_STEREO)?2:1) << std::endl;
		std::cout << "Number of bytes pr. sample: " << bytes_per_sample << std::endl;
		std::cout << "---------------------------------" << std::endl;

		throw CL_Error("Invalid wave file format");
	}
}

CL_Streamed_MikModSample_Session::~CL_Streamed_MikModSample_Session()
{
	Player_Stop();
}

bool CL_Streamed_MikModSample_Session::eof() const
{
	return !Player_Active();
}

bool CL_Streamed_MikModSample_Session::set_position(int pos)
{
	Player_SetPosition(pos);
	return true; // (Player_GetPosition() == pos); does not exist, what to do?
}

bool CL_Streamed_MikModSample_Session::play()
{
	if (Player_Active())
		return false;
		
	Player_Start(module);
	return Player_Active() != 0;
}

void CL_Streamed_MikModSample_Session::stop()
{
	Player_Stop();
}

int CL_Streamed_MikModSample_Session::get_data(void *data_ptr, int data_requested)
{
	MikMod_Update();
	int read = VC_WriteBytes((SBYTE*)data_ptr,data_requested);

	return read;
}

int CL_Streamed_MikModSample_Session::get_frequency() const
{
	return sample_freq;
}

SoundFormat CL_Streamed_MikModSample_Session::get_format() const
{
	return sample_format;
}

// EOF //