1 /**
2     \file  ADM_audioWriteWav
3     \brief Writer
4 
5     copyright            : (C) 2011 by mean
6     email                : fixounet@free.fr
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #include "ADM_default.h"
19 #include "ADM_audioStream.h"
20 #include "ADM_audioWriteWav.h"
21 /**
22     \fn ctor
23 */
ADM_audioWriteWav()24 ADM_audioWriteWav::ADM_audioWriteWav()
25 {
26     writter=NULL;
27     dataPosition=0;
28 }
29 /**
30     \fn writeHeader
31 */
writeHeader(ADM_audioStream * stream)32 bool ADM_audioWriteWav::writeHeader(ADM_audioStream *stream)
33 {
34           writter = new riffWritter("RIFF", _file);
35           writter->begin("WAVE");
36           // Write wavheader...
37           WAVHeader wh,*p;
38           p=stream->getInfo();
39           wh.encoding=WAV_PCM;
40           wh.channels=p->channels;
41           wh.blockalign=p->channels*2;
42           wh.byterate=p->channels*p->frequency*2;
43           wh.frequency=p->frequency;
44           wh.bitspersample=16;
45 
46           writter->writeWavHeader("fmt ",&wh);
47           writter->write32("data");
48           dataPosition=writter->tell();
49           writter->write32( (uint32_t )0);
50           return true;
51 }
52 /**
53     \fn updateHeader
54 */
55 
updateHeader(void)56 bool ADM_audioWriteWav::updateHeader(void)
57 {
58         uint64_t theEnd=ftello(_file);
59         fseeko(_file,dataPosition,SEEK_SET);
60         writter->write32((uint32_t)(theEnd-dataPosition));
61         return true;
62 }
63 
64 
65 /**
66     \fn close
67 */
68 
close(void)69 bool ADM_audioWriteWav::close(void)
70 {
71     if(_file)
72     {
73         updateHeader();
74     }
75     if(writter)
76     {
77         writter->end();
78         delete writter;
79         writter=NULL;
80     }
81     return ADM_audioWrite::close();
82 }
83 /**
84     \fn init
85     \brief write wavHeader
86 */
87 
init(ADM_audioStream * stream,const char * fileName)88 bool ADM_audioWriteWav::init(ADM_audioStream *stream, const char *fileName)
89 {
90     if(false==ADM_audioWrite::init(stream,fileName)) return false;
91     return writeHeader(stream);
92 }
93 //EOF
94