summaryrefslogtreecommitdiffstats
path: root/krec/krecfile.h
blob: 86c29c779c1f637076e21bcc802d19df6d07738b (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
/***************************************************************************
    copyright            : (C) 2003 by Arnold Krille
    email                : arnold@arnoldarts.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; version 2 of the License.               *
 *                                                                         *
 ***************************************************************************/

#ifndef KRECFILE_H
#define KRECFILE_H

#include <tqobject.h>
#include <tqstring.h>
#include <tqvaluelist.h>

#include <arts/common.h>

class KRecBuffer;
class KTempDir;
class KSimpleConfig;
class TQFile;
class KRecFileViewWidget;

class KRecFile : virtual public TQObject {
   TQ_OBJECT
  
	friend class KRecFileWidget;
public:
	KRecFile( TQObject*, const char* =0 );
	KRecFile( const TQString &, TQObject*, const char* =0 );
	~KRecFile();

	TQString filename();
	void filename( const TQString &);

	bool saved() const { return _saved; }
	int samplerate() const { return _samplerate; }
	int channels() const { return _channels; }
	int bits() const { return _bits; }

	TQIODevice::Offset offsetSize() const { return samplesToOffset( _size ); }
	TQIODevice::Offset offsetPosition() const { return samplesToOffset( _pos ); }

	int size() const { return _size; }
	int position() const { return _pos; }

	/// Calculates an offset into a frames-value depending on the files settings (channels and bits)
	int offsetToSamples( TQIODevice::Offset ) const;
	TQIODevice::Offset samplesToOffset( int ) const;
public slots:
	/// Creates a new empty buffer and marks it for recording.
	void newBuffer();

	/// Deletes the buffer from the file.
	void deleteBuffer( KRecBuffer* =0 );

	void writeData( Arts::mcopbyte* data, uint length );
	void writeData( TQByteArray* );
	void writeData( TQByteArray& );

	void save( const TQString & );
	void exportwave( const TQString & );

	/// Fills a TQByteArray with values from the specified Offset.
	TQByteArray* getData( int );

	void getData( TQByteArray& );

	void newPos( int );

private slots:
	void newPos( KRecBuffer*, TQIODevice::Offset );
	void newSize( KRecBuffer*, TQIODevice::Offset );
signals:
	/// Position signals
	void posChanged( int );
	void sizeChanged( int );
	/// The end of the file is reached, you should stop reading.
	void endReached();

	/// GUI-Signals
	void sNewBuffer( KRecBuffer* );
	void sDeleteBuffer( KRecBuffer* );

	void filenameChanged( const TQString & );
private:
	void saveProps();
	void loadProps();
	/**
		Creates a new buffer for a given file
		@param filename Name of the file
	*/
	void newBuffer( const TQString &filename );
	/**
		Adds buffer to the buffers.
		@param buffer The to be added buffer
	*/
	void newBuffer( KRecBuffer* buffer );

	/// Get the top-most-buffer at the specified position.
	int getTopBuffer_int( int );
	KRecBuffer* getTopBuffer_buffer( int );

	bool _saved;
	TQString _filename;
	int _samplerate, _channels, _bits;
	int _currentBuffer;
	TQValueList<KRecBuffer*> _buffers;
	KTempDir *_dir;
	KSimpleConfig *_config;
	int _pos, _size;

	void init();
};

class TQFile;
class TQDir;
class TQFileInfo;
class TDEConfig;
class TQDataStream;

class KRecBuffer : virtual public TQObject {
   TQ_OBJECT
  
public:
	KRecBuffer( const TQString &, int, bool, KRecFile*, const char* =0 );
	~KRecBuffer();

	// After how many samples in the File this Buffer starts...
	int startpos() const;

	TQIODevice::Offset size() const;
	int sizeInSamples() const { return _krecfile->offsetToSamples( size() ); }

	/**
		Writes out its Config
		It doesn't set the group.
		@param config the TDEConfig where the data gets written
	*/
	void writeConfig( TDEConfig* config );

	/**
		restores a KRecBuffer from Config
		The group has to be set the right way.
		@param config The configuration thats read
		@param dir The directory where the file is saved
	*/
	static KRecBuffer* fromConfig( TDEConfig* config, TQDir* dir, KRecFile* p, const char* n=0 );

	TQString filename() const;

	bool active() const;

	TQString title() const;
	TQString comment() const;
public slots:
	/// writes the data into the buffer
	void writeData( Arts::mcopbyte* data, uint length );
	void writeData( TQByteArray* );
	void writeData( TQByteArray& );
	/// gets the data from the stream
	void getData( TQByteArray& );

	void setPos( TQIODevice::Offset );

	void setActive( bool );

	void deleteBuffer();

	void setTitle( const TQString & );
	void setComment( const TQString & );

	/// Returns the sample at the specified position and channel.
	float getSample( int pos, int channel );
	float* getsamples( int start, int end, int channel );
signals:
	void posChanged( KRecBuffer*, TQIODevice::Offset );
	void sizeChanged( KRecBuffer*, TQIODevice::Offset );
	void activeChanged( bool );

	/// Is emitted when something has changed.
	void somethingChanged();

	void deleteSelf( KRecBuffer* );
private:
	KRecFile* _krecfile;
	TQFile* _file;
	TQDataStream* _stream;
	TQFileInfo* _fileinfo;
	bool _open, _active;
	TQIODevice::Offset _pos;
	int _start;
	TQString _title, _comment;
};

#endif