-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathFileWvOut.cpp
140 lines (111 loc) · 3.42 KB
/
FileWvOut.cpp
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
/***************************************************/
/*! \class FileWvOut
\brief STK audio file output class.
This class inherits from WvOut. It provides a "tick-level"
interface to the FileWrite class.
FileWvOut writes samples to an audio file and supports
multi-channel data. It is important to distinguish the tick()
method that outputs a single sample to all channels in a sample
frame from the overloaded one that takes a reference to an
StkFrames object for multi-channel and/or multi-frame data.
See the FileWrite class for a description of the supported audio
file formats.
Currently, FileWvOut is non-interpolating and the output rate is
always Stk::sampleRate().
by Perry R. Cook and Gary P. Scavone, 1995--2023.
*/
/***************************************************/
#include "FileWvOut.h"
namespace stk {
FileWvOut :: FileWvOut( unsigned int bufferFrames )
:bufferFrames_( bufferFrames )
{
}
FileWvOut::FileWvOut( std::string fileName, unsigned int nChannels, FileWrite::FILE_TYPE type, Stk::StkFormat format, unsigned int bufferFrames )
:bufferFrames_( bufferFrames )
{
this->openFile( fileName, nChannels, type, format );
}
FileWvOut :: ~FileWvOut()
{
this->closeFile();
}
void FileWvOut :: closeFile( void )
{
if ( file_.isOpen() ) {
// Output any remaining samples in the buffer before closing.
if ( bufferIndex_ > 0 ) {
data_.resize( bufferIndex_, data_.channels() );
file_.write( data_ );
}
file_.close();
frameCounter_ = 0;
}
}
void FileWvOut :: openFile( std::string fileName,
unsigned int nChannels,
FileWrite::FILE_TYPE type,
Stk::StkFormat format )
{
closeFile();
if ( nChannels < 1 ) {
oStream_ << "FileWvOut::openFile: the channels argument must be greater than zero!";
handleError( StkError::FUNCTION_ARGUMENT );
}
// An StkError can be thrown by the FileWrite class here.
file_.open( fileName, nChannels, type, format );
// Allocate new memory if necessary.
data_.resize( bufferFrames_, nChannels );
bufferIndex_ = 0;
iData_ = 0;
}
void FileWvOut :: incrementFrame( void )
{
frameCounter_++;
bufferIndex_++;
if ( bufferIndex_ == bufferFrames_ ) {
file_.write( data_ );
bufferIndex_ = 0;
iData_ = 0;
}
}
void FileWvOut :: tick( const StkFloat sample )
{
#if defined(_STK_DEBUG_)
if ( !file_.isOpen() ) {
oStream_ << "FileWvOut::tick(): no file open!";
handleError( StkError::WARNING );
return;
}
#endif
unsigned int nChannels = data_.channels();
StkFloat input = sample;
clipTest( input );
for ( unsigned int j=0; j<nChannels; j++ )
data_[iData_++] = input;
this->incrementFrame();
}
void FileWvOut :: tick( const StkFrames& frames )
{
#if defined(_STK_DEBUG_)
if ( !file_.isOpen() ) {
oStream_ << "FileWvOut::tick(): no file open!";
handleError( StkError::WARNING );
return;
}
if ( data_.channels() != frames.channels() ) {
oStream_ << "FileWvOut::tick(): incompatible channel value in StkFrames argument!";
handleError( StkError::FUNCTION_ARGUMENT );
}
#endif
unsigned int iFrames = 0;
unsigned int j, nChannels = data_.channels();
for ( unsigned int i=0; i<frames.frames(); i++ ) {
for ( j=0; j<nChannels; j++ ) {
data_[iData_] = frames[iFrames++];
clipTest( data_[iData_++] );
}
this->incrementFrame();
}
}
} // stk namespace