Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[920a14]: src / SignalManager.h Maximize Restore History

Download this file

SignalManager.h    370 lines (306 with data), 11.1 kB

  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
/***************************************************************************
SignalManager.h - manager class for multi-channel signals
-------------------
begin : Sun Oct 15 2000
copyright : (C) 2000 by Thomas Eschenbacher
email : Thomas.Eschenbacher@gmx.de
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef _SIGNAL_MANAGER_H_
#define _SIGNAL_MANAGER_H_ 1
#define processid 0
#define stopprocess 1
#define samplepointer 2
#include <qobject.h>
#include <qarray.h>
#include <qlist.h>
#include <stdio.h>
#include "mt/SignalProxy.h"
class ProgressDialog;
class QBitmap;
class Signal;
class TimeOperation;
typedef struct {
int rate;
int channels;
int bits_per_sample;
const char *device;
int bufbase;
} playback_param_t;
/**
* The SignalManager class manages multi-channel signals.
*/
class SignalManager : public QObject
{
Q_OBJECT
public:
SignalManager(Signal *sig);
SignalManager(const char *filename, int type = 0);
SignalManager(unsigned int length, int rate,
unsigned int channels = 1);
virtual ~SignalManager();
bool executeCommand(const char *command);
int setSoundParams(int audio, int bitspersample,
unsigned int channels, int rate, int bufbase);
/**
* Internally used for playback.
* @param device file descriptor of the opened playback device
* @param param parameters used for playback
* @param buffer pointer to a sample buffer used for playback
* @param bufsize size of the buffer in bytes
* @param start position where playback should start
* @param loop true: looping instead of single play
*/
void playback(int device, playback_param_t &param,
unsigned char *buffer, unsigned int bufsize,
unsigned int start, bool loop);
/**
* Determines the maximum and minimum values of a given range
* of samples.
* @param channel index of the channel
* @param max receives the highest sample value (most positive)
* @param min receives the lowest sample value (most negative)
* @param begin start of the sample range (inclusive)
* @param len number of samples to inspect
*/
void getMaxMin(unsigned int channel, int &max, int& min,
unsigned int begin, unsigned int len);
/**
* Returns a QBitmap with an overview of all currently present
* signals.
* @param width width of the resulting bitmap in pixels
* @param height height of the resutling bitmap in pixels
* @param offset index of the first sample
* @param length number of samples
*/
QBitmap *overview(unsigned int width, unsigned int height,
unsigned int offset, unsigned int length);
/**
* Returns the current sample resolution in bits per sample
*/
int getBitsPerSample();
/** Returns the current sample rate in samples per second */
inline int getRate()
{
return rate;
};
/** Returns the current number of channels */
inline unsigned int channels()
{
return m_channels;
};
/**
* Returns the number of samples in the current signal. Will be
* zero if no signal is loaded.
*/
unsigned int getLength();
/**
* Returns the start of the selection (inclusive).
*/
inline unsigned int getLMarker()
{
return lmarker;
};
/**
* Returns the end of the selection (inclusive).
*/
inline unsigned int getRMarker()
{
return rmarker;
};
inline Signal *getSignal (int channel)
{
return signal.at(channel);
};
/**
* Returns an array of indices of currently selected channels.
*/
const QArray<unsigned int> selectedChannels();
/**
* Returns the value of one single sample of a specified channel.
* If the channel does not exist or the index of the sample is
* out of range the return value will be zero.
* @param channel index if the channel [0...N-1]
* @param offset sample offset [0...length-1]
* @return value of the sample
*/
int singleSample(unsigned int channel, unsigned int offset);
/**
* Returns the value of one single sample averaged over all active channels.
* If no channel do exist or the index of the sample is out of range the
* return value will be zero. If the optional list of channels is omitted,
* the sample will be averaged over all currently selected channels.
* @param offset sample offset [0...length-1]
* @param channels an array of channel numbers, optional
* @return value of the sample
*/
int averageSample(unsigned int offset,
const QArray<unsigned int> *channels = 0);
void save(const char *filename, int bits, bool selection);
/**
* Exports ascii file with one sample per line and only one channel.
*/
void exportAscii(const char *name);
void appendChannel(Signal *);
/**
* Sets the internal markers and promotes them to all channels.
* @param l left marker [0...length-1]
* @param r right marker [0...length-1]
*/
void setRange(unsigned int l, unsigned int r);
bool promoteCommand (const char *command);
/**
* Toggles the selection flag of a channel.
* @param channel index of the channel [0..N-1]
*/
void toggleChannel(const unsigned int channel);
public slots:
/**
* Starts playback.
* @param start position where playback should start
* @param loop true: looping instead of single play
*/
void startplay(unsigned int start, bool loop);
/**
* Stops playback.
*/
void stopplay();
signals:
/**
* Emitted if a command has to be executed by
* the next higher instance.
* @param command the command to be executed
*/
void sigCommand(const char *command);
/**
* Indicates that the signal data within a range
* has changed.
* @param lmarker leftmost sample or -1 for "up to the left"
* @param rmarker rightmost sample or -1 for "up to the right"
*/
void signalChanged(int lmarker, int rmarker);
/**
* Signals that a channel has been added/inserted. The channels
* at and after this position (if any) have moved to channel+1.
* @param channel index of the new channel [0...N-1]
*/
void sigChannelAdded(unsigned int channel);
/**
* Signals that a channel has been deleted. All following channels
* are shifted one channel down.
* @param channel index of the deleted channel [0...N-1]
*/
void sigChannelDeleted(unsigned int channel);
/**
* Indicates a change in the position of the playback pointer
* during playback.
*/
void sigPlaybackPos(unsigned int pos);
/**
* Indicates that playback is done.
*/
void sigPlaybackDone();
private slots:
/**
* Informs us that the last command of a plugin has completed.
*/
void commandDone();
/**
* Called from the playback thread to notify about a new
* playback pointer.
* \internal
*/
void updatePlaybackPos();
/**
* emits sigPlaybackDone() at end of playback
*/
void forwardPlaybackDone();
private:
void initialize();
ProgressDialog *createProgressDialog(TimeOperation *operation,
const char *caption);
/**
* Searches for the wav data chunk in a wav file.
* @param sigin pointer to the already opened file
* @return the position of the data or 0 if failed
*/
int findWavChunk(FILE *sigin);
/**
* Imports ascii file with one sample per line and only one
* channel. Everything that cannot be parsed by strod will be ignored.
* @return 0 if succeeded or error number if failed
*/
int loadAscii();
/**
* Loads a .wav-File.
* @return 0 if succeeded or a negative error number if failed:
* -ENOENT if file does not exist,
* -ENODATA if the file has no data chunk or is zero-length,
* -EMEDIUMTYPE if the file has an invalid/unsupported format
*/
int loadWav();
/**
* Appends a new empty channel to the current signal.
* It has the same length as the first channel.
*/
void addChannel();
/**
* Removes a channel from the signal.
*/
void deleteChannel(unsigned int channel);
private:
/**
* Reads in the wav data chunk from a .wav-file. It creates
* a new empty Signal for each channel and fills it with
* data read from an opened file. The file's actual position
* must already be set to the correct position.
* @param sigin pointer to the already opened file
* @param length number of samples to be read
* @param channels number of channels [1..n]
* @param number of bits per sample [8,16,24,...]
* @return 0 if succeeded or error number if failed
*/
int loadWavChunk(FILE *sigin, unsigned int length,
unsigned int channels, int bits);
/**
* Writes the chunk with the signal to a .wav file (not including
* the header!).
* @param sigout pointer to the already opened file
* @param length number of samples to be written
* @param channels number of channels [1..n]
* @param number of bits per sample [8,16,24,...]
* @return 0 if succeeded or error number if failed
*/
int writeWavChunk(FILE *sigout, unsigned int begin, unsigned int length,
int bits);
char *name;
/** list of all channels (signals) */
QList<Signal> signal;
unsigned int lmarker;
unsigned int rmarker;
unsigned int m_channels;
int rate; //sampling rate being used
/**
* Signal proxy that brings the current playback position
* out of the playback thread.
*/
SignalProxy1<unsigned int> m_spx_playback_pos;
/**
* Signal proxy that signals the end of playback out
* of the playback thread.
*/
SignalProxy<void> m_spx_playback_done;
/** error string from the playback thread */
const char *m_playback_error;
public:
/** buffer for communication with the soundcard access functions (play) */
unsigned int msg[4];
};
#endif // _SIGNAL_MANAGER_H_