foobar2000 SDK  2015-08-03
Public Member Functions | Private Member Functions | Private Attributes
CWavWriter

#include <writer_wav.h>

Public Member Functions

void close ()
 
void finalize (abort_callback &p_abort)
 
audio_chunk::spec_t get_spec () const
 
bool is_open () const
 
void open (const char *p_path, const wavWriterSetup_t &p_setup, abort_callback &p_abort)
 
void open (service_ptr_t< file > p_file, const wavWriterSetup_t &p_setup, abort_callback &p_abort)
 
void write (const audio_chunk &p_chunk, abort_callback &p_abort)
 

Private Member Functions

size_t align (abort_callback &abort)
 
t_uint32 chunkOverhead () const
 
t_uint32 idOverhead () const
 
bool is64 () const
 
void writeID (const GUID &id, abort_callback &abort)
 
void writeSize (t_uint64 size, abort_callback &abort)
 

Private Attributes

t_uint64 m_bytes_written
 
service_ptr_t< filem_file
 
t_uint64 m_offset_fix1
 
t_uint64 m_offset_fix1_delta
 
t_uint64 m_offset_fix2
 
service_ptr_t< audio_postprocessorm_postprocessor
 
mem_block_container_aligned_incremental_impl< 16 > m_postprocessor_output
 
wavWriterSetup_t m_setup
 
bool m_wfxe
 

Detailed Description

Definition at line 27 of file writer_wav.h.

Member Function Documentation

size_t CWavWriter::align ( abort_callback abort)
private

Definition at line 117 of file writer_wav.cpp.

117  {
118  t_uint8 dummy[8] = {};
119  const t_uint32 val = is64() ? 8 : 2;
120  t_filesize pos = m_file->get_position(abort);
121  t_size delta = (val - (pos%val)) % val;
122  if (delta > 0) m_file->write(dummy, delta, abort);
123  return delta;
124 }
uint8_t t_uint8
Definition: int_types.h:9
bool is64() const
Definition: writer_wav.h:40
size_t t_size
Definition: int_types.h:48
service_ptr_t< file > m_file
Definition: writer_wav.h:44
t_uint64 t_filesize
Type used for file size related variables.
Definition: filesystem.h:8
uint32_t t_uint32
Definition: int_types.h:5
t_uint32 CWavWriter::chunkOverhead ( ) const
inlineprivate

Definition at line 41 of file writer_wav.h.

41 {return is64() ? 24 : 8;}
bool is64() const
Definition: writer_wav.h:40
void CWavWriter::close ( )

Definition at line 271 of file writer_wav.cpp.

272 {
273  m_file.release();
275 }
service_ptr_t< audio_postprocessor > m_postprocessor
Definition: writer_wav.h:45
void release()
Definition: service.h:109
service_ptr_t< file > m_file
Definition: writer_wav.h:44
void CWavWriter::finalize ( abort_callback p_abort)

Definition at line 254 of file writer_wav.cpp.

255 {
256  if (m_file.is_valid())
257  {
258  const size_t alignG = align(p_abort);
259 
260  if (m_file->can_seek()) {
261  m_file->seek(m_offset_fix1,p_abort);
262  writeSize(m_bytes_written + alignG + m_offset_fix1_delta, p_abort);
263  m_file->seek(m_offset_fix2,p_abort);
264  writeSize(m_bytes_written, p_abort);
265  }
266  m_file.release();
267  }
269 }
t_uint64 m_offset_fix1_delta
Definition: writer_wav.h:48
t_uint64 m_bytes_written
Definition: writer_wav.h:48
service_ptr_t< audio_postprocessor > m_postprocessor
Definition: writer_wav.h:45
bool is_valid() const
Definition: service.h:119
t_uint64 m_offset_fix2
Definition: writer_wav.h:48
size_t align(abort_callback &abort)
Definition: writer_wav.cpp:117
void release()
Definition: service.h:109
service_ptr_t< file > m_file
Definition: writer_wav.h:44
void writeSize(t_uint64 size, abort_callback &abort)
Definition: writer_wav.cpp:105
t_uint64 m_offset_fix1
Definition: writer_wav.h:48
audio_chunk::spec_t CWavWriter::get_spec ( ) const

Definition at line 277 of file writer_wav.cpp.

277  {
278  audio_chunk::spec_t spec = {};
282  return spec;
283 }
unsigned m_samplerate
Definition: writer_wav.h:10
wavWriterSetup_t m_setup
Definition: writer_wav.h:46
unsigned m_channel_mask
Definition: writer_wav.h:10
unsigned m_channels
Definition: writer_wav.h:10
t_uint32 CWavWriter::idOverhead ( ) const
inlineprivate

Definition at line 42 of file writer_wav.h.

42 {return is64() ? 16 : 4;}
bool is64() const
Definition: writer_wav.h:40
bool CWavWriter::is64 ( ) const
inlineprivate

Definition at line 40 of file writer_wav.h.

40 {return m_setup.m_wave64;}
wavWriterSetup_t m_setup
Definition: writer_wav.h:46
bool CWavWriter::is_open ( ) const
inline

Definition at line 35 of file writer_wav.h.

35 { return m_file.is_valid(); }
bool is_valid() const
Definition: service.h:119
service_ptr_t< file > m_file
Definition: writer_wav.h:44
void CWavWriter::open ( const char *  p_path,
const wavWriterSetup_t p_setup,
abort_callback p_abort 
)

Definition at line 126 of file writer_wav.cpp.

127 {
128  service_ptr_t<file> l_file;
129  filesystem::g_open_write_new(l_file,p_path,p_abort);
130  open(l_file,p_setup,p_abort);
131 }
void open(const char *p_path, const wavWriterSetup_t &p_setup, abort_callback &p_abort)
Definition: writer_wav.cpp:126
void CWavWriter::open ( service_ptr_t< file p_file,
const wavWriterSetup_t p_setup,
abort_callback p_abort 
)

Definition at line 136 of file writer_wav.cpp.

137 {
138  m_file = p_file;
139  m_setup = p_setup;
140 
142 
143  if (!audio_chunk::g_is_valid_sample_rate(m_setup.m_samplerate)) throw exception_io_data();
144 
145  if (m_setup.m_bpsValid > m_setup.m_bps) throw exceptionBadBitDepth();
146 
147  if (m_setup.m_float)
148  {
149  if (m_setup.m_bps != 32 && m_setup.m_bps != 64) throw exceptionBadBitDepth();
150  if (m_setup.m_bpsValid != m_setup.m_bps) throw exceptionBadBitDepth();
151  }
152  else
153  {
154  if (m_setup.m_bps != 8 && m_setup.m_bps != 16 && m_setup.m_bps != 24 && m_setup.m_bps != 32) throw exceptionBadBitDepth();
155  if (m_setup.m_bpsValid < 1) throw exceptionBadBitDepth();
156  }
157 
158  m_wfxe = m_setup.needWFXE();
159 
160  writeID(guid_RIFF, p_abort);
161  m_offset_fix1 = m_file->get_position(p_abort);
162  writeSize(~0, p_abort);
163 
164  writeID(guid_WAVE, p_abort);
165 
166  writeID(guid_FMT, p_abort);
167  if (m_wfxe) {
168  writeSize(sizeof(WAVEFORMATEXTENSIBLE),p_abort);
169 
170  WAVEFORMATEXTENSIBLE wfxe;
171  m_setup.setup_wfxe(wfxe);
172  m_file->write_object(&wfxe,sizeof(wfxe),p_abort);
173  } else {
174  writeSize(sizeof(PCMWAVEFORMAT),p_abort);
175 
176  WAVEFORMATEX wfx;
177  m_setup.setup_wfx(wfx);
178  m_file->write_object(&wfx,/* blah */ sizeof(PCMWAVEFORMAT),p_abort);
179  }
180  align(p_abort);
181 
182  writeID(guid_DATA, p_abort);
183  m_offset_fix2 = m_file->get_position(p_abort);
184  writeSize(~0, p_abort);
185  m_offset_fix1_delta = m_file->get_position(p_abort) - chunkOverhead();
186 
187 
188  m_bytes_written = 0;
189 
190  if (!m_setup.m_float)
191  {
192  m_postprocessor = standard_api_create_t<audio_postprocessor>();
193  }
194 }
t_uint64 m_offset_fix1_delta
Definition: writer_wav.h:48
void writeID(const GUID &id, abort_callback &abort)
Definition: writer_wav.cpp:92
void setup_wfx(WAVEFORMATEX &p_wfx)
Definition: writer_wav.cpp:70
t_uint64 m_bytes_written
Definition: writer_wav.h:48
service_ptr_t< audio_postprocessor > m_postprocessor
Definition: writer_wav.h:45
void setup_wfxe(WAVEFORMATEXTENSIBLE &p_wfx)
Definition: writer_wav.cpp:81
static const GUID guid_WAVE
Definition: writer_wav.cpp:5
unsigned m_bpsValid
Definition: writer_wav.h:10
unsigned m_samplerate
Definition: writer_wav.h:10
static const GUID guid_FMT
Definition: writer_wav.cpp:6
bool m_wfxe
Definition: writer_wav.h:47
t_uint64 m_offset_fix2
Definition: writer_wav.h:48
static unsigned g_count_channels(unsigned p_config)
Counts channels specified by channel map.
wavWriterSetup_t m_setup
Definition: writer_wav.h:46
size_t align(abort_callback &abort)
Definition: writer_wav.cpp:117
unsigned m_bps
Definition: writer_wav.h:10
unsigned m_channel_mask
Definition: writer_wav.h:10
static const GUID guid_DATA
Definition: writer_wav.cpp:7
static const GUID guid_RIFF
Definition: writer_wav.cpp:4
service_ptr_t< file > m_file
Definition: writer_wav.h:44
unsigned m_channels
Definition: writer_wav.h:10
void writeSize(t_uint64 size, abort_callback &abort)
Definition: writer_wav.cpp:105
bool needWFXE() const
Definition: writer_wav.cpp:21
t_uint32 chunkOverhead() const
Definition: writer_wav.h:41
t_uint64 m_offset_fix1
Definition: writer_wav.h:48
static bool g_is_valid_sample_rate(t_uint32 p_val)
Definition: audio_chunk.h:11
void CWavWriter::write ( const audio_chunk p_chunk,
abort_callback p_abort 
)

Definition at line 196 of file writer_wav.cpp.

197 {
198  if (p_chunk.get_channels() != m_setup.m_channels
200  || p_chunk.get_srate() != m_setup.m_samplerate
201  ) throw exception_unexpected_audio_format_change();
202 
203 
204  if (m_setup.m_float)
205  {
206  switch(m_setup.m_bps)
207  {
208  case 32:
209  {
210 #if audio_sample_size == 32
211  t_size bytes = p_chunk.get_sample_count() * p_chunk.get_channels() * sizeof(audio_sample);
212  m_file->write_object(p_chunk.get_data(),bytes,p_abort);
213  m_bytes_written += bytes;
214 #else
215  enum {tempsize = 256};
216  float temp[tempsize];
217  t_size todo = p_chunk.get_sample_count() * p_chunk.get_channels();
218  const audio_sample * readptr = p_chunk.get_data();
219  while(todo > 0)
220  {
221  unsigned n,delta = todo;
222  if (delta > tempsize) delta = tempsize;
223  for(n=0;n<delta;n++)
224  temp[n] = (float)(*(readptr++));
225  unsigned bytes = delta * sizeof(float);
226  m_file->write_object_e(temp,bytes,p_abort);
227  m_bytes_written += bytes;
228  todo -= delta;
229  }
230 #endif
231  }
232  break;
233 #if 0
234  case 64:
235  {
236  unsigned bytes = p_chunk.get_sample_count() * p_chunk.get_channels() * sizeof(audio_sample);
237  m_file->write_object_e(p_chunk.get_data(),bytes,p_abort);
238  m_bytes_written += bytes;
239  }
240  break;
241 #endif
242  default:
243  throw exception_io_data();
244  }
245  }
246  else
247  {
251  }
252 }
t_uint64 m_bytes_written
Definition: writer_wav.h:48
service_ptr_t< audio_postprocessor > m_postprocessor
Definition: writer_wav.h:45
virtual unsigned get_channels() const =0
Retrieves channel count of contained audio data.
unsigned m_bpsValid
Definition: writer_wav.h:10
unsigned m_samplerate
Definition: writer_wav.h:10
virtual unsigned get_srate() const =0
Retrieves sample rate of contained audio data.
virtual unsigned get_channel_config() const =0
Retrieves channel map of contained audio data. Conditions where number of channels specified by chann...
wavWriterSetup_t m_setup
Definition: writer_wav.h:46
size_t t_size
Definition: int_types.h:48
virtual audio_sample * get_data()=0
Retrieves audio data buffer pointer (non-const version). Returned pointer is for temporary use only; ...
unsigned m_bps
Definition: writer_wav.h:10
unsigned m_channel_mask
Definition: writer_wav.h:10
mem_block_container_aligned_incremental_impl< 16 > m_postprocessor_output
Definition: writer_wav.h:49
float audio_sample
Definition: audio_sample.h:6
service_ptr_t< file > m_file
Definition: writer_wav.h:44
unsigned m_channels
Definition: writer_wav.h:10
virtual t_size get_sample_count() const =0
Retrieves number of valid samples in the buffer. Note that a "sample" means a unit of interleaved PC...
void CWavWriter::writeID ( const GUID id,
abort_callback abort 
)
private

Definition at line 92 of file writer_wav.cpp.

92  {
93  if (is64()) {
94  m_file->write_object_t(id, abort);
95  } else {
96  for(t_size walk = 0; walk < PFC_TABSIZE(RIFF_chunks); ++walk) {
97  if (id == RIFF_chunks[walk].m_guid) {
98  m_file->write(RIFF_chunks[walk].m_name, 4, abort); return;
99  }
100  }
101  uBugCheck();
102  }
103 }
bool is64() const
Definition: writer_wav.h:40
size_t t_size
Definition: int_types.h:48
service_ptr_t< file > m_file
Definition: writer_wav.h:44
PFC_NORETURN void SHARED_EXPORT uBugCheck()
static const RIFF_chunk_desc RIFF_chunks[]
Definition: writer_wav.cpp:14
void CWavWriter::writeSize ( t_uint64  size,
abort_callback abort 
)
private

Definition at line 105 of file writer_wav.cpp.

105  {
106  if (is64()) {
107  if (size != ~0) size += 24;
108  m_file->write_lendian_t(size, abort);
109  } else {
110  t_uint32 clipped;
111  if (size > 0xFFFFFFFF) clipped = 0xFFFFFFFF;
112  else clipped = (t_uint32) size;
113  m_file->write_lendian_t(clipped, abort);
114  }
115 }
bool is64() const
Definition: writer_wav.h:40
service_ptr_t< file > m_file
Definition: writer_wav.h:44
uint32_t t_uint32
Definition: int_types.h:5

Field Documentation

t_uint64 CWavWriter::m_bytes_written
private

Definition at line 48 of file writer_wav.h.

service_ptr_t<file> CWavWriter::m_file
private

Definition at line 44 of file writer_wav.h.

t_uint64 CWavWriter::m_offset_fix1
private

Definition at line 48 of file writer_wav.h.

t_uint64 CWavWriter::m_offset_fix1_delta
private

Definition at line 48 of file writer_wav.h.

t_uint64 CWavWriter::m_offset_fix2
private

Definition at line 48 of file writer_wav.h.

service_ptr_t<audio_postprocessor> CWavWriter::m_postprocessor
private

Definition at line 45 of file writer_wav.h.

mem_block_container_aligned_incremental_impl<16> CWavWriter::m_postprocessor_output
private

Definition at line 49 of file writer_wav.h.

wavWriterSetup_t CWavWriter::m_setup
private

Definition at line 46 of file writer_wav.h.

bool CWavWriter::m_wfxe
private

Definition at line 47 of file writer_wav.h.


The documentation for this class was generated from the following files: