summaryrefslogtreecommitdiffstats
path: root/flow/gslpp/datahandle.h
blob: 7cab930efa42bc335c5e3fa4d822e73e0245168d (plain)
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
    /*

    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.

    */

#ifndef GSLPP_DATAHANDLE_H
#define GSLPP_DATAHANDLE_H

#include <gsl/gsldatahandle.h>
#include <gsl/gslloader.h>
#include <string>

namespace GSL
{
	class WaveFileInfo;
	class WaveDataHandle;
	class WaveDescription;

	class DataHandle
	{
	protected:
		GslDataHandle *handle_;

		void clearHandle();
		void copyFrom(const DataHandle &other);

		DataHandle(GslDataHandle *handle);

	public:
		static DataHandle null() { return DataHandle(); }

		DataHandle(): handle_(0) {}

		DataHandle(const DataHandle &other);

		DataHandle &operator =(const DataHandle &other);

		bool operator ==(const DataHandle &other) const;

		bool isNull() const;

		virtual ~DataHandle();

		/**
		 * returns error code with errno meaning
		 */
		virtual gint open();

		virtual void close();

		bool isOpen() const;

		/**
		 * read from this datahandle and fill buffer pointed to by
		 * values with sample data from the range
		 * [valueOffset..valueOffset+valueCount-1]
		 *
		 * returns number of sample values actually read, -1 on error
		 * (errno _may_ be useful then)
		 */
		virtual GslLong read(GslLong valueOffset, GslLong valueCount, gfloat *values);

		/**
		 * Create a new data handle by chopping off headCutValueCount
		 * values at the start, and tailCutValueCount values at the
		 * end of this data handle.
		 */
		DataHandle createCropped(GslLong headCutValueCount,
								 GslLong tailCutValueCount);

		/**
		 * Create a new data handle by removing the values
		 * [cutOffset..cutOffset+cutValueCount-1].
		 */
		DataHandle createCut(GslLong cutOffset,
							 GslLong cutValueCount);

		/**
		 * Create a new data handle which contains the same values as
		 * this one but in reversed order.
		 */
		DataHandle createReversed();

		// this will maybe be made private in the future, having a
		// better interface instead
		GslDataCache *createGslDataCache();

		GslLong valueCount() const;

		guint bitDepth() const;

		guint channelCount() const;
	};

	class WaveChunkDescription
	{
		friend class WaveDescription;

		const GslWaveDsc    *parent_;
		guint                parentIndex_;

		WaveChunkDescription(const GslWaveDsc *parent, guint index);

	public:
		float oscillatorFrequency();
		float mixerFrequency();

		GslWaveLoopType loopType();
		GslLong loopStart();	/* sample offset */
		GslLong loopEnd();	/* sample offset */
		guint loopCount();

		WaveDataHandle createDataHandle();
	};

	class WaveDescription
	{
		friend class WaveFileInfo; // for construction of WaveDescriptions

		mutable GslWaveFileInfo    *parentInfo_;  // mutable for ensurePointer()
		std::string                 name_;
		guint                       parentIndex_;
		mutable GslWaveDsc         *desc_;
		mutable GslErrorType        error_;

		void ensurePointer() const; // lazy construction

		void copyFrom(const WaveDescription &other);

		WaveDescription(GslWaveFileInfo *parent, guint index, const std::string &name);

	public:
		~WaveDescription();

		WaveDescription(const WaveDescription &other);

		WaveDescription &operator =(const WaveDescription &other);

		const std::string &name() const;

		GslErrorType error() const;

		guint chunkCount() const;

		WaveChunkDescription chunkDescription(guint index) const;

		guint channelCount() const;
	};

	class WaveFileInfo
	{
		GslWaveFileInfo *info_;
		GslErrorType     error_;
		std::string      filename_;

		void copyFrom(const WaveFileInfo &other);

	public:
		WaveFileInfo(const std::string &filename);

		~WaveFileInfo();

		WaveFileInfo(const WaveFileInfo &other);

		WaveFileInfo &operator =(const WaveFileInfo &other);

		guint waveCount() const;

		std::string waveName(guint index) const;

		WaveDescription waveDescription(guint index);

		GslErrorType error() const;
	};

	class WaveDataHandle: public DataHandle
	{
		friend class WaveChunkDescription;

		GslErrorType     error_;

		float oscillatorFrequency_;
		float mixerFrequency_;

		WaveDataHandle();
		WaveDataHandle(const GslWaveDsc *waveDesc, guint chunkIndex = 0);

	public:
		WaveDataHandle(const std::string& filename,
					   guint waveIndex = 0,
					   guint chunkIndex = 0);

		static WaveDataHandle null() { return WaveDataHandle(); }

		GslErrorType error() const;

		float oscillatorFrequency() const;

		float mixerFrequency() const;
	};
}

#endif // GSLPP_DATAHANDLE_H