summaryrefslogtreecommitdiffstats
path: root/krec/krecexport_wave.cpp
blob: 64e256495cf90bd44631c9e2e2e3e9670e743433 (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
/***************************************************************************
    copyright            : (C) 2003 by Arnold Krille
    email                : [email protected]
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.               *
 *                                                                         *
 ***************************************************************************/

#include "krecexport_wave.h"
#include "krecexport_wave.moc"

#include "krecglobal.h"

#include <kdebug.h>
#include <tdetempfile.h>
#include <tqfile.h>
#include <tqtimer.h>
#include <kgenericfactory.h>

K_EXPORT_COMPONENT_FACTORY( libkrecexport_wave, KGenericFactory<KRecExport_Wave> )

KRecExport_Wave krecExportWave( 0 );

KRecExport_Wave::KRecExport_Wave( TQObject* p, const char* n, const TQStringList& )
  : KRecExportItem( p,n )
  , _file( 0 )
{
kdDebug( 60005 ) << k_funcinfo << endl;
	registerAtGlobal( this );
kdDebug( 60005 ) << "Registered Exports: " << KRecGlobal::the()->exportFormats() << endl;
}
KRecExport_Wave::~KRecExport_Wave() {
kdDebug( 60005 ) << k_funcinfo << endl;
}

KRecExport_Wave* KRecExport_Wave::newItem() {
kdDebug( 60005 ) << k_funcinfo << endl;
	return new KRecExport_Wave( 0 );
}

TQStringList KRecExport_Wave::extensions() {
	//kdDebug( 60005 ) << k_funcinfo << endl;
	TQStringList tmp;
	tmp << "*.wav" << "*.WAV";
	return tmp;
}

bool KRecExport_Wave::initialize( const TQString &filename ) {
kdDebug( 60005 ) << k_funcinfo << endl;
	if ( !_file ) {
		_file = new TQFile( filename );
		if ( _file->open( IO_Raw|IO_WriteOnly ) ) {
			/// Write an empty Wave-header...
			for ( int i=0; i<44; i++ )
				_file->putch( 0 );
		} else return false;
		return true;
	} else return false;
}
bool KRecExport_Wave::process() {
	//kdDebug( 60005 ) << k_funcinfo << running << endl;
	if ( _file ) {
		if ( running() ) {
			TQByteArray bytearray( 4096 );
			emit getData( bytearray );
			_file->writeBlock( bytearray );
			TQTimer::singleShot( 10, this, TQT_SLOT( process() ) );
		}
		return true;
	} else return false;
}
bool KRecExport_Wave::finalize() {
kdDebug( 60005 ) << k_funcinfo << endl;
	if ( _file ) {
		// PCM-Compatible WAVE Header
		// bytes		variable			description
		// 0  - 3		'RIFF'
		// 4  - 7		wRiffLength			length of file minus the 8 byte riff header
		// 8  - 11		'WAVE'
		// 12 - 15		'fmt '
		// 16 - 19		wFmtSize			length of format chunk minus 8 byte header
		// 20 - 21		wFormatTag			identifies PCM, ULAW etc
		// 22 - 23		wChannels			number of channels
		// 24 - 27		dwSamplesPerSecond	samples per second per channel
		// 28 - 31		dwAvgBytesPerSec	non-trivial for compressed formats
		// 32 - 33		wBlockAlign			basic block size
		// 34 - 35		wBitsPerSample		non-trivial for compressed formats
		// 36 - 39		'data'
		// 40 - 43		dwDataLength		length of data chunk minus 8 byte header
		// 44 - (dwDataLength + 43)			the data

		// Basic 16b,2c,44kHz Wave-Header
		char riffHeader[] =
		{
		  '\x52', '\x49', '\x46', '\x46', // 0  "AIFF"
		  '\x00', '\x00', '\x00', '\x00', // 4  wavSize
		  '\x57', '\x41', '\x56', '\x45', // 8  "WAVE"
		  '\x66', '\x6d', '\x74', '\x20', // 12 "fmt "
		  '\x10', '\x00', '\x00', '\x00', // 16
		  '\x01', '\x00', '\x02', '\x00', // 20
		  '\x44', '\xac', '\x00', '\x00', // 24
		  '\x10', '\xb1', '\x02', '\x00', // 28
		  '\x04', '\x00', '\x10', '\x00', // 32
		  '\x64', '\x61', '\x74', '\x61', // 36 "data"
		  '\x00', '\x00', '\x00', '\x00'  // 40 byteCount
		};

		long wavSize = _file->size() - 8;
		write32( riffHeader, wavSize, 4 );

		write16( riffHeader, channels(), 22 );
		write32( riffHeader, samplingRate(), 24 );
		write16( riffHeader, bits(), 34 );

		long byteCount = wavSize - 44;
		write32( riffHeader, byteCount, 40 );

		_file->at( 0 );
		_file->writeBlock( riffHeader, 44 );
		_file->at( _file->size() );

		_file->close();
		delete _file;
		_file = 0;

		return true;
	} else return false;
}

// vim:sw=4:ts=4