File: wavefilewriter.cpp

package info (click to toggle)
linux-minidisc 0.9.14-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,640 kB
  • ctags: 1,572
  • sloc: ansic: 6,357; cpp: 2,708; python: 2,451; perl: 866; sh: 136; makefile: 8
file content (153 lines) | stat: -rw-r--r-- 4,452 bytes parent folder | download | duplicates (2)
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
141
142
143
144
145
146
147
148
149
150
151
152
153

/**
 * wavfilewriter: Helper class to write .wav file headers
 * Copyright (C) 2016 Thomas Perl <m@thp.io>
 *
 * 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 **/

#include "wavefilewriter.h"

#include <QIODevice>
#include <QtEndian>
#include <QDebug>

/**
 * Based on the file format description at http://soundfile.sapp.org/doc/WaveFormat/
 **/

struct RIFFHeader {
    char riff[4]; // "RIFF"
    uint32_t total_size;
    char format[4]; // "WAVE"
};

struct ChunkHeader {
    char id[4]; // "fmt " for format chunk, "data" for samples
    uint32_t size;
};

struct FormatChunk {
    int16_t audioFormat;
    uint16_t numChannels;
    uint32_t sampleRate;
    uint32_t bytesPerSecond;
    uint16_t blockAlign;
    uint16_t bitsPerSample;
};

WaveFileWriter::WaveFileWriter()
    : output()
    , updateSize(false)
{
}

WaveFileWriter::~WaveFileWriter()
{
    close();
}

bool
WaveFileWriter::open(const QString &filename, int sampleRate, int sampleSize, int channels)
{
    output.close();
    output.setFileName(filename);
    output.open(QIODevice::WriteOnly);

    RIFFHeader hdr;
    ChunkHeader chk;
    FormatChunk fmt;

    // RIFF header
    memcpy(hdr.riff, "RIFF", 4);
    hdr.total_size = 0; // To be filled later
    memcpy(hdr.format, "WAVE", 4);
    if ((size_t)output.write((char *)&hdr, sizeof(hdr)) != sizeof(hdr)) {
        return false;
    }

    // Format chunk header
    memcpy(chk.id, "fmt ", 4);
    chk.size = qToLittleEndian(uint32_t(sizeof(fmt)));
    if ((size_t)output.write((char *)&chk, sizeof(chk)) != sizeof(chk)) {
        return false;
    }

    // Format chunk
    fmt.audioFormat = qToLittleEndian(int16_t(1)); // PCM
    fmt.numChannels = qToLittleEndian(uint32_t(channels));
    fmt.sampleRate = qToLittleEndian(uint32_t(sampleRate));
    fmt.bytesPerSecond = qToLittleEndian(uint32_t(channels * (sampleSize / 8) * sampleRate));
    fmt.blockAlign = qToLittleEndian(uint32_t(channels * (sampleSize / 8)));
    fmt.bitsPerSample = qToLittleEndian(uint32_t(sampleSize));
    if ((size_t)output.write((char *)&fmt, sizeof(fmt)) != sizeof(fmt)) {
        return false;
    }

    // Data chunk header
    memcpy(chk.id, "data", 4);
    chk.size = 0; // To be filled later
    if ((size_t)output.write((char *)&chk, sizeof(chk)) != sizeof(chk)) {
        return false;
    }

    updateSize = true;
    return true;
}

bool
WaveFileWriter::write_signed_big_endian(const int16_t *data, size_t samples)
{
    int16_t tmp[samples];
    for (size_t i=0; i<samples; i++) {
        tmp[i] = qToLittleEndian(qFromBigEndian(data[i]));
    }
    return ((size_t)output.write(reinterpret_cast<const char *>(tmp), sizeof(tmp)) == sizeof(tmp));
}

static bool
updateU32LESizeField(QFile &file, size_t offset, uint32_t new_value)
{
    if (!file.seek(offset)) {
        qWarning() << "Could not seek to update field";
        return false;
    }

    // The "remaining" size is calculated from after the value (subtract offset + value size)
    new_value = qToLittleEndian(uint32_t(new_value - (offset + sizeof(new_value))));
    if ((size_t)file.write((char *)&new_value, sizeof(new_value)) != sizeof(new_value)) {
        qWarning() << "Could not update field in file";
        return false;
    }

    return true;
}

void
WaveFileWriter::close()
{
    // File size in the RIFF header
    size_t riff_header_offset = offsetof(RIFFHeader, total_size);
    // Chunk size in the "data" chunk
    size_t data_header_offset = sizeof(RIFFHeader) + sizeof(ChunkHeader) + sizeof(FormatChunk) + offsetof(ChunkHeader, size);

    if (updateSize &&
            updateU32LESizeField(output, riff_header_offset, output.size()) &&
            updateU32LESizeField(output, data_header_offset, output.size())) {
        updateSize = false;
    }

    output.close();
}