1 /****************************************************************************
2 **
3 ** Copyright (C) 2017 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 **   * Redistributions of source code must retain the above copyright
25 **     notice, this list of conditions and the following disclaimer.
26 **   * Redistributions in binary form must reproduce the above copyright
27 **     notice, this list of conditions and the following disclaimer in
28 **     the documentation and/or other materials provided with the
29 **     distribution.
30 **   * Neither the name of The Qt Company Ltd nor the names of its
31 **     contributors may be used to endorse or promote products derived
32 **     from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
51 #include "wavefilewriter.h"
52 
53 struct chunk
54 {
55     char        id[4];
56     quint32     size;
57 };
58 
59 struct RIFFHeader
60 {
61     chunk       descriptor;     // "RIFF"
62     char        type[4];        // "WAVE"
63 };
64 
65 struct WAVEHeader
66 {
67     chunk       descriptor;
68     quint16     audioFormat;
69     quint16     numChannels;
70     quint32     sampleRate;
71     quint32     byteRate;
72     quint16     blockAlign;
73     quint16     bitsPerSample;
74 };
75 
76 struct DATAHeader
77 {
78     chunk       descriptor;
79 };
80 
81 struct CombinedHeader
82 {
83     RIFFHeader  riff;
84     WAVEHeader  wave;
85     DATAHeader  data;
86 };
87 static const int HeaderLength = sizeof(CombinedHeader);
88 
89 
WaveFileWriter(QObject * parent)90 WaveFileWriter::WaveFileWriter(QObject *parent)
91     : QObject(parent)
92     , m_dataLength(0)
93 {
94 }
95 
~WaveFileWriter()96 WaveFileWriter::~WaveFileWriter()
97 {
98     close();
99 }
100 
open(const QString & fileName,const QAudioFormat & format)101 bool WaveFileWriter::open(const QString& fileName, const QAudioFormat& format)
102 {
103     if (file.isOpen())
104         return false; // file already open
105 
106     if (format.codec() != "audio/pcm" || format.sampleType() != QAudioFormat::SignedInt)
107         return false; // data format is not supported
108 
109     file.setFileName(fileName);
110     if (!file.open(QIODevice::WriteOnly))
111         return false; // unable to open file for writing
112 
113     if (!writeHeader(format))
114         return false;
115 
116     m_format = format;
117     return true;
118 }
119 
write(const QAudioBuffer & buffer)120 bool WaveFileWriter::write(const QAudioBuffer &buffer)
121 {
122     if (buffer.format() != m_format)
123         return false; // buffer format has changed
124 
125     qint64 written = file.write((const char *)buffer.constData(), buffer.byteCount());
126     m_dataLength += written;
127     return written == buffer.byteCount();
128 }
129 
close()130 bool WaveFileWriter::close()
131 {
132     bool result = false;
133     if (file.isOpen()) {
134         Q_ASSERT(m_dataLength < INT_MAX);
135         result = writeDataLength();
136 
137         m_dataLength = 0;
138         file.close();
139     }
140     return result;
141 }
142 
writeHeader(const QAudioFormat & format)143 bool WaveFileWriter::writeHeader(const QAudioFormat &format)
144 {
145     // check if format is supported
146     if (format.byteOrder() == QAudioFormat::BigEndian || format.sampleType() != QAudioFormat::SignedInt)
147         return false;
148 
149     CombinedHeader header;
150     memset(&header, 0, HeaderLength);
151 
152 #ifndef Q_LITTLE_ENDIAN
153     // only implemented for LITTLE ENDIAN
154     return false;
155 #else
156     // RIFF header
157     memcpy(header.riff.descriptor.id, "RIFF", 4);
158     header.riff.descriptor.size = 0; // this will be updated with correct duration:
159                                      // m_dataLength + HeaderLength - 8
160     // WAVE header
161     memcpy(header.riff.type, "WAVE", 4);
162     memcpy(header.wave.descriptor.id, "fmt ", 4);
163     header.wave.descriptor.size = quint32(16);
164     header.wave.audioFormat = quint16(1);
165     header.wave.numChannels = quint16(format.channelCount());
166     header.wave.sampleRate = quint32(format.sampleRate());
167     header.wave.byteRate = quint32(format.sampleRate() * format.channelCount() * format.sampleSize() / 8);
168     header.wave.blockAlign = quint16(format.channelCount() * format.sampleSize() / 8);
169     header.wave.bitsPerSample = quint16(format.sampleSize());
170 
171     // DATA header
172     memcpy(header.data.descriptor.id,"data", 4);
173     header.data.descriptor.size = 0; // this will be updated with correct data length: m_dataLength
174 
175     return (file.write(reinterpret_cast<const char *>(&header), HeaderLength) == HeaderLength);
176 #endif
177 }
178 
writeDataLength()179 bool WaveFileWriter::writeDataLength()
180 {
181 #ifndef Q_LITTLE_ENDIAN
182     // only implemented for LITTLE ENDIAN
183     return false;
184 #endif
185 
186     if (file.isSequential())
187         return false;
188 
189     // seek to RIFF header size, see header.riff.descriptor.size above
190     if (!file.seek(4))
191         return false;
192 
193     quint32 length = m_dataLength + HeaderLength - 8;
194     if (file.write(reinterpret_cast<const char *>(&length), 4) != 4)
195         return false;
196 
197     // seek to DATA header size, see header.data.descriptor.size above
198     if (!file.seek(40))
199         return false;
200 
201     return file.write(reinterpret_cast<const char *>(&m_dataLength), 4) == 4;
202 }
203