WavDumper.h (4327B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #if !defined(WavDumper_h_) 8 # define WavDumper_h_ 9 # include <ByteWriter.h> 10 # include <mozilla/Atomics.h> 11 # include <mozilla/DebugOnly.h> 12 # include <mozilla/EndianUtils.h> 13 # include <mozilla/Sprintf.h> 14 # include <nsString.h> 15 # include <nsTArray.h> 16 # include <stdint.h> 17 # include <stdio.h> 18 19 /** 20 * If MOZ_DUMP_AUDIO is set, this dumps a file to disk containing the output of 21 * an audio stream, in 16bits integers. 22 * 23 * The sandbox needs to be disabled for this to work. 24 */ 25 class WavDumper { 26 public: 27 WavDumper() = default; 28 ~WavDumper() { 29 if (mFile) { 30 fclose(mFile); 31 } 32 } 33 34 void Open(const char* aBaseName, uint32_t aChannels, uint32_t aRate) { 35 using namespace mozilla; 36 37 if (!getenv("MOZ_DUMP_AUDIO")) { 38 return; 39 } 40 41 static mozilla::Atomic<int> sDumpedAudioCount(0); 42 43 char buf[100]; 44 SprintfLiteral(buf, "%s-%d.wav", aBaseName, ++sDumpedAudioCount); 45 OpenExplicit(buf, aChannels, aRate); 46 } 47 48 void OpenExplicit(const char* aPath, uint32_t aChannels, uint32_t aRate) { 49 # ifdef XP_WIN 50 nsAutoString widePath = NS_ConvertUTF8toUTF16(aPath); 51 mFile = _wfopen(widePath.get(), L"wb"); 52 # else 53 mFile = fopen(aPath, "wb"); 54 # endif 55 if (!mFile) { 56 NS_WARNING("Could not open file to DUMP a wav. Is sandboxing disabled?"); 57 return; 58 } 59 const uint8_t riffHeader[] = { 60 // RIFF header 61 0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00, 0x57, 0x41, 0x56, 0x45, 62 // fmt chunk. We always write 16-bit samples. 63 0x66, 0x6d, 0x74, 0x20, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0xFF, 0xFF, 64 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x10, 0x00, 65 // data chunk 66 0x64, 0x61, 0x74, 0x61, 0xFE, 0xFF, 0xFF, 0x7F}; 67 AutoTArray<uint8_t, sizeof(riffHeader)> header; 68 mozilla::ByteWriter<mozilla::LittleEndian> writer(header); 69 static const int CHANNEL_OFFSET = 22; 70 static const int SAMPLE_RATE_OFFSET = 24; 71 static const int BLOCK_ALIGN_OFFSET = 32; 72 73 mozilla::DebugOnly<bool> rv; 74 // Then number of bytes written in each iteration. 75 uint32_t written = 0; 76 for (size_t i = 0; i != sizeof(riffHeader);) { 77 switch (i) { 78 case CHANNEL_OFFSET: 79 rv = writer.WriteU16(aChannels); 80 written = 2; 81 MOZ_ASSERT(rv); 82 break; 83 case SAMPLE_RATE_OFFSET: 84 rv = writer.WriteU32(aRate); 85 written = 4; 86 MOZ_ASSERT(rv); 87 break; 88 case BLOCK_ALIGN_OFFSET: 89 rv = writer.WriteU16(aChannels * 2); 90 written = 2; 91 MOZ_ASSERT(rv); 92 break; 93 default: 94 // copy from the riffHeader struct above 95 rv = writer.WriteU8(riffHeader[i]); 96 written = 1; 97 MOZ_ASSERT(rv); 98 break; 99 } 100 i += written; 101 } 102 (void)fwrite(header.Elements(), header.Length(), 1, mFile); 103 } 104 105 template <typename T> 106 void Write(const T* aBuffer, uint32_t aSamples) { 107 if (!mFile) { 108 return; 109 } 110 if (aBuffer) { 111 WriteDumpFileHelper(aBuffer, aSamples); 112 } else { 113 constexpr size_t blockSize = 128; 114 T block[blockSize] = {}; 115 for (size_t remaining = aSamples; remaining;) { 116 size_t toWrite = std::min(remaining, blockSize); 117 fwrite(block, sizeof(T), toWrite, mFile); 118 remaining -= toWrite; 119 } 120 } 121 fflush(mFile); 122 } 123 124 private: 125 void WriteDumpFileHelper(const int16_t* aInput, size_t aSamples) { 126 (void)fwrite(aInput, sizeof(int16_t), aSamples, mFile); 127 } 128 129 void WriteDumpFileHelper(const float* aInput, size_t aSamples) { 130 using namespace mozilla; 131 132 AutoTArray<uint8_t, 1024 * 2> buf; 133 mozilla::ByteWriter<mozilla::LittleEndian> writer(buf); 134 for (uint32_t i = 0; i < aSamples; ++i) { 135 mozilla::DebugOnly<bool> rv = 136 writer.WriteU16(int16_t(aInput[i] * 32767.0f)); 137 MOZ_ASSERT(rv); 138 } 139 (void)fwrite(buf.Elements(), buf.Length(), 1, mFile); 140 } 141 142 FILE* mFile = nullptr; 143 }; 144 145 #endif // WavDumper_h_