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
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#if !defined(WavDumper_h_)
# define WavDumper_h_
# include <stdio.h>
# include <stdint.h>
# include <nsTArray.h>
# include <nsString.h>
# include <mozilla/Unused.h>
# include <mozilla/Atomics.h>
# include <mozilla/DebugOnly.h>
# include <mozilla/Sprintf.h>
# include <ByteWriter.h>
/**
* If MOZ_DUMP_AUDIO is set, this dumps a file to disk containing the output of
* an audio stream, in 16bits integers.
*
* The sandbox needs to be disabled for this to work.
*/
class WavDumper {
public:
WavDumper() = default;
~WavDumper() {
if (mFile) {
fclose(mFile);
}
}
void Open(const char* aBaseName, uint32_t aChannels, uint32_t aRate) {
using namespace mozilla;
if (!getenv("MOZ_DUMP_AUDIO")) {
return;
}
static mozilla::Atomic<int> sDumpedAudioCount(0);
char buf[100];
SprintfLiteral(buf, "%s-%d.wav", aBaseName, ++sDumpedAudioCount);
OpenExplicit(buf, aChannels, aRate);
}
void OpenExplicit(const char* aPath, uint32_t aChannels, uint32_t aRate) {
# ifdef XP_WIN
nsAutoString widePath = NS_ConvertUTF8toUTF16(aPath);
mFile = _wfopen(widePath.get(), L"wb");
# else
mFile = fopen(aPath, "wb");
# endif
if (!mFile) {
NS_WARNING("Could not open file to DUMP a wav. Is sandboxing disabled?");
return;
}
const uint8_t riffHeader[] = {
// RIFF header
0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00, 0x57, 0x41, 0x56, 0x45,
// fmt chunk. We always write 16-bit samples.
0x66, 0x6d, 0x74, 0x20, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x10, 0x00,
// data chunk
0x64, 0x61, 0x74, 0x61, 0xFE, 0xFF, 0xFF, 0x7F};
AutoTArray<uint8_t, sizeof(riffHeader)> header;
mozilla::ByteWriter<mozilla::LittleEndian> writer(header);
static const int CHANNEL_OFFSET = 22;
static const int SAMPLE_RATE_OFFSET = 24;
static const int BLOCK_ALIGN_OFFSET = 32;
mozilla::DebugOnly<bool> rv;
// Then number of bytes written in each iteration.
uint32_t written = 0;
for (size_t i = 0; i != sizeof(riffHeader);) {
switch (i) {
case CHANNEL_OFFSET:
rv = writer.WriteU16(aChannels);
written = 2;
MOZ_ASSERT(rv);
break;
case SAMPLE_RATE_OFFSET:
rv = writer.WriteU32(aRate);
written = 4;
MOZ_ASSERT(rv);
break;
case BLOCK_ALIGN_OFFSET:
rv = writer.WriteU16(aChannels * 2);
written = 2;
MOZ_ASSERT(rv);
break;
default:
// copy from the riffHeader struct above
rv = writer.WriteU8(riffHeader[i]);
written = 1;
MOZ_ASSERT(rv);
break;
}
i += written;
}
mozilla::Unused << fwrite(header.Elements(), header.Length(), 1, mFile);
}
template <typename T>
void Write(const T* aBuffer, uint32_t aSamples) {
if (!mFile) {
return;
}
WriteDumpFileHelper(aBuffer, aSamples);
}
private:
void WriteDumpFileHelper(const int16_t* aInput, size_t aSamples) {
mozilla::Unused << fwrite(aInput, sizeof(int16_t), aSamples, mFile);
fflush(mFile);
}
void WriteDumpFileHelper(const float* aInput, size_t aSamples) {
using namespace mozilla;
AutoTArray<uint8_t, 1024 * 2> buf;
mozilla::ByteWriter<mozilla::LittleEndian> writer(buf);
for (uint32_t i = 0; i < aSamples; ++i) {
mozilla::DebugOnly<bool> rv =
writer.WriteU16(int16_t(aInput[i] * 32767.0f));
MOZ_ASSERT(rv);
}
mozilla::Unused << fwrite(buf.Elements(), buf.Length(), 1, mFile);
fflush(mFile);
}
FILE* mFile = nullptr;
};
#endif // WavDumper_h_
|