File: datahandle.cpp

package info (click to toggle)
arts 1.5.9-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 8,436 kB
  • ctags: 9,848
  • sloc: ansic: 44,670; cpp: 33,776; sh: 10,486; perl: 3,470; makefile: 372; yacc: 347; lex: 160
file content (412 lines) | stat: -rw-r--r-- 9,158 bytes parent folder | download | duplicates (4)
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
    /*

    Copyright (C) 2002 Hans Meine
                       hans_meine@gmx.net

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.
  
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.

    */

#include "datahandle.h"
#include "../../mcop/debug.h"

namespace GSL {

void DataHandle::copyFrom(const DataHandle &other)
{
	handle_ = other.handle_;
	if(handle_)
		gsl_data_handle_ref(handle_);
}

DataHandle::DataHandle(GslDataHandle *handle)
	: handle_(handle)
{
}

DataHandle::DataHandle(const DataHandle &other) { copyFrom(other); }

DataHandle &DataHandle::operator =(const DataHandle &other)
{
	if(!( other == *this )) {
		if(handle_)
			gsl_data_handle_unref(handle_);
		copyFrom(other);
	}
	return *this;
}

bool DataHandle::operator ==(const DataHandle &other) const
{
	return handle_ == other.handle_;
}

bool DataHandle::isNull() const
{
	return handle_ == 0;
}

DataHandle::~DataHandle()
{
	if(handle_)
		gsl_data_handle_unref(handle_);
}

gint DataHandle::open()
{
	arts_return_val_if_fail(handle_ != 0, -1);

	arts_debug("open()ing datahandle (open_count before: %d)..",
			   handle_->open_count);

	return gsl_data_handle_open(handle_);
}

void DataHandle::close()
{
	arts_return_if_fail(handle_ != 0);

	arts_debug("close()ing datahandle (open_count before: %d)..",
			   handle_->open_count);

	gsl_data_handle_close(handle_);
}

bool DataHandle::isOpen() const
{
	if(!handle_)
		return false;

	return handle_->open_count != 0;
}

GslLong DataHandle::read(GslLong valueOffset, GslLong valueCount, gfloat *values)
{
	arts_return_val_if_fail(handle_ != 0, 0);

	return gsl_data_handle_read(handle_, valueOffset, valueCount, values);
}

DataHandle DataHandle::createCropped(GslLong headCutValueCount,
									 GslLong tailCutValueCount)
{
	arts_return_val_if_fail(handle_ != 0, null());

	return DataHandle(gsl_data_handle_new_crop(handle_,
											   headCutValueCount, tailCutValueCount));
}

DataHandle DataHandle::createCut(GslLong cutOffset,
								 GslLong cutValueCount)
{
	arts_return_val_if_fail(handle_ != 0, null());

	return DataHandle(gsl_data_handle_new_cut(handle_, cutOffset,
											  cutValueCount));
}

DataHandle DataHandle::createReversed()
{
	arts_return_val_if_fail(handle_ != 0, null());

	return DataHandle(gsl_data_handle_new_reverse(handle_));
}

GslDataCache *DataHandle::createGslDataCache()
{
	arts_debug("wanna have cache with padding %d for each of %d channels..",
			   gsl_get_config()->wave_chunk_padding,
			   channelCount());
	return gsl_data_cache_from_dhandle(handle_,
									   gsl_get_config()->wave_chunk_padding *
									   channelCount());
}

GslLong DataHandle::valueCount() const
{
	arts_return_val_if_fail(handle_ != 0, 0);
	arts_return_val_if_fail(isOpen(), 0);

	return handle_->setup.n_values;
}

guint DataHandle::channelCount() const
{
	arts_return_val_if_fail(handle_ != 0, 0);
	arts_return_val_if_fail(isOpen(), 0);

	return handle_->setup.n_channels;
}

guint DataHandle::bitDepth() const
{
	arts_return_val_if_fail(handle_ != 0, 0);
	arts_return_val_if_fail(isOpen(), 0);

	return handle_->setup.bit_depth;
}

// ----------------------------------------------------------------------------

WaveChunkDescription::WaveChunkDescription(const GslWaveDsc *parent, guint index)
	: parent_(parent), parentIndex_(index)
{
	if(index>parent->n_chunks)
	{
		arts_debug("wrong index given to WaveChunkDescription constructor, "
				   "using 0 instead..");
		parentIndex_ = 0;
	}
}

float WaveChunkDescription::oscillatorFrequency()
{
	return parent_->chunks[parentIndex_].osc_freq;
}

float WaveChunkDescription::mixerFrequency()
{
	return parent_->chunks[parentIndex_].mix_freq;
}

GslWaveLoopType WaveChunkDescription::loopType()
{
	return parent_->chunks[parentIndex_].loop_type;
}

GslLong WaveChunkDescription::loopStart()
{
	return parent_->chunks[parentIndex_].loop_start;
}

GslLong WaveChunkDescription::loopEnd()
{
	return parent_->chunks[parentIndex_].loop_end;
}

guint WaveChunkDescription::loopCount()
{
	return parent_->chunks[parentIndex_].loop_count;
}

WaveDataHandle WaveChunkDescription::createDataHandle()
{
	return WaveDataHandle(parent_, parentIndex_);
}

// ----------------------------------------------------------------------------

/**
 * We use GslWaveFileInfos' refcounting - probably this lazy
 * construction happens only once, where the object is used. After
 * copying it would have to be constructed again, but that's not
 * likely to happen. (?)
 */
void WaveDescription::ensurePointer() const
{
	if(!desc_)
		desc_ = gsl_wave_dsc_load(parentInfo_, parentIndex_, &error_);
}

void WaveDescription::copyFrom(const WaveDescription &other)
{
	parentInfo_ = other.parentInfo_;
	parentIndex_ = other.parentIndex_;
	gsl_wave_file_info_ref(other.parentInfo_);
}

// internal, private
WaveDescription::WaveDescription(GslWaveFileInfo *parent, guint index,
								 const std::string &name)
	: parentInfo_(parent), name_(name), parentIndex_(index),
	  desc_(0), error_(GSL_ERROR_NONE)
{
	gsl_wave_file_info_ref(parentInfo_);
}

WaveDescription::~WaveDescription()
{
	if(desc_)
		gsl_wave_dsc_free(desc_);
	gsl_wave_file_info_unref(parentInfo_);
}

WaveDescription::WaveDescription(const WaveDescription &other)
	: desc_(0), error_(GSL_ERROR_NONE)
{
	copyFrom(other);
}

WaveDescription &WaveDescription::operator =(const WaveDescription &other)
{
	if(desc_)
		gsl_wave_dsc_free(desc_);
	gsl_wave_file_info_unref(parentInfo_);
	copyFrom(other);
	return *this;
}

const std::string &WaveDescription::name() const
{
	return name_;
}

GslErrorType WaveDescription::error() const
{
	ensurePointer();
	return error_;
}

guint WaveDescription::chunkCount() const
{
	ensurePointer();
	return desc_ ? desc_->n_chunks : 0;
}

WaveChunkDescription WaveDescription::chunkDescription(guint index) const
{
	ensurePointer();
	return WaveChunkDescription(desc_, index);
}

guint WaveDescription::channelCount() const
{
	ensurePointer();
	return desc_ ? desc_->n_channels : 0;
}

// ----------------------------------------------------------------------------

void WaveFileInfo::copyFrom(const WaveFileInfo &other)
{
	info_ = other.info_;
	filename_ = other.filename_;
	if(info_)
		gsl_wave_file_info_ref(info_);

	error_ = other.error_;
}

WaveFileInfo::WaveFileInfo(const std::string &filename)
	: info_(0), error_(GSL_ERROR_NONE), filename_(filename)
{
	info_ = gsl_wave_file_info_load(filename.c_str(), &error_);
}

WaveFileInfo::~WaveFileInfo()
{
	if(info_)
		gsl_wave_file_info_unref(info_);
}

WaveFileInfo::WaveFileInfo(const WaveFileInfo &other)
{
	copyFrom(other);
}

WaveFileInfo &WaveFileInfo::operator =(const WaveFileInfo &other)
{
	if(info_)
		gsl_wave_file_info_unref(info_);
	copyFrom(other);
	return *this;
}

guint WaveFileInfo::waveCount() const
{
	return info_ ? info_->n_waves : 0;
}

std::string WaveFileInfo::waveName(guint index) const
{
	if(index >= waveCount())
		return "";
	return info_->waves[index].name;
}

WaveDescription WaveFileInfo::waveDescription(guint index)
{
	return WaveDescription(info_, index, waveName(index));
}

GslErrorType WaveFileInfo::error() const
{
	return error_;
}

// ----------------------------------------------------------------------------

WaveDataHandle::WaveDataHandle()
	: DataHandle(NULL),
	  oscillatorFrequency_(0),
	  mixerFrequency_(0)
{
}

WaveDataHandle::WaveDataHandle(const GslWaveDsc *waveDesc, guint chunkIndex)
	: DataHandle(NULL),
	  oscillatorFrequency_(0),
	  mixerFrequency_(0)
{
	handle_ = gsl_wave_handle_create(const_cast<GslWaveDsc *>(waveDesc),
									 chunkIndex, &error_);
	if(error() == GSL_ERROR_NONE)
	{
		oscillatorFrequency_ = waveDesc->chunks[chunkIndex].osc_freq;
		mixerFrequency_ = waveDesc->chunks[chunkIndex].mix_freq;
	}
}

WaveDataHandle::WaveDataHandle(const std::string& filename,
							   guint waveIndex,
							   guint chunkIndex)
	: DataHandle(NULL),
	  oscillatorFrequency_(0),
	  mixerFrequency_(0)
{
	GSL::WaveFileInfo info(filename);
	error_ = info.error();

	if(!info.error())
	{
		GSL::WaveDescription desc= info.waveDescription(waveIndex);
		error_ = desc.error();

		if(!desc.error() && (desc.chunkCount() > chunkIndex))
		{
			GSL::WaveChunkDescription chunkDesc= desc.chunkDescription(chunkIndex);

			(*this) = chunkDesc.createDataHandle();
		}
	}
}

GslErrorType WaveDataHandle::error() const
{
	return error_;
}

float WaveDataHandle::oscillatorFrequency() const
{
	return oscillatorFrequency_;
}

float WaveDataHandle::mixerFrequency() const
{
	return mixerFrequency_;
}

}