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 <stdio.h> 10 # include <stdint.h> 11 # include <nsTArray.h> 12 # include <mozilla/Unused.h> 13 # include <mozilla/Atomics.h> 14 # include <mozilla/DebugOnly.h> 15 # include <mozilla/Sprintf.h> 16 # include <ByteWriter.h> 17 18 /** 19 * If MOZ_DUMP_AUDIO is set, this dumps a file to disk containing the output of 20 * an audio stream, in 16bits integers. 21 * 22 * The sandbox needs to be disabled for this to work. 23 */ 24 class WavDumper { 25 public: 26 WavDumper() = default; ~WavDumper()27 ~WavDumper() { 28 if (mFile) { 29 fclose(mFile); 30 } 31 } 32 Open(const char * aBaseName,uint32_t aChannels,uint32_t aRate)33 void Open(const char* aBaseName, uint32_t aChannels, uint32_t aRate) { 34 using namespace mozilla; 35 36 if (!getenv("MOZ_DUMP_AUDIO")) { 37 return; 38 } 39 40 static mozilla::Atomic<int> sDumpedAudioCount(0); 41 42 char buf[100]; 43 SprintfLiteral(buf, "%s-%d.wav", aBaseName, ++sDumpedAudioCount); 44 OpenExplicit(buf, aChannels, aRate); 45 } 46 OpenExplicit(const char * aPath,uint32_t aChannels,uint32_t aRate)47 void OpenExplicit(const char* aPath, uint32_t aChannels, uint32_t aRate) { 48 mFile = fopen(aPath, "wb"); 49 if (!mFile) { 50 NS_WARNING("Could not open file to DUMP a wav. Is sandboxing disabled?"); 51 return; 52 } 53 const uint8_t riffHeader[] = { 54 // RIFF header 55 0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00, 0x57, 0x41, 0x56, 0x45, 56 // fmt chunk. We always write 16-bit samples. 57 0x66, 0x6d, 0x74, 0x20, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0xFF, 0xFF, 58 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x10, 0x00, 59 // data chunk 60 0x64, 0x61, 0x74, 0x61, 0xFE, 0xFF, 0xFF, 0x7F}; 61 AutoTArray<uint8_t, sizeof(riffHeader)> header; 62 mozilla::ByteWriter<mozilla::LittleEndian> writer(header); 63 static const int CHANNEL_OFFSET = 22; 64 static const int SAMPLE_RATE_OFFSET = 24; 65 static const int BLOCK_ALIGN_OFFSET = 32; 66 67 mozilla::DebugOnly<bool> rv; 68 // Then number of bytes written in each iteration. 69 uint32_t written = 0; 70 for (size_t i = 0; i != sizeof(riffHeader);) { 71 switch (i) { 72 case CHANNEL_OFFSET: 73 rv = writer.WriteU16(aChannels); 74 written = 2; 75 MOZ_ASSERT(rv); 76 break; 77 case SAMPLE_RATE_OFFSET: 78 rv = writer.WriteU32(aRate); 79 written = 4; 80 MOZ_ASSERT(rv); 81 break; 82 case BLOCK_ALIGN_OFFSET: 83 rv = writer.WriteU16(aChannels * 2); 84 written = 2; 85 MOZ_ASSERT(rv); 86 break; 87 default: 88 // copy from the riffHeader struct above 89 rv = writer.WriteU8(riffHeader[i]); 90 written = 1; 91 MOZ_ASSERT(rv); 92 break; 93 } 94 i += written; 95 } 96 mozilla::Unused << fwrite(header.Elements(), header.Length(), 1, mFile); 97 } 98 99 template <typename T> Write(const T * aBuffer,uint32_t aSamples)100 void Write(const T* aBuffer, uint32_t aSamples) { 101 if (!mFile) { 102 return; 103 } 104 WriteDumpFileHelper(aBuffer, aSamples); 105 } 106 107 private: WriteDumpFileHelper(const int16_t * aInput,size_t aSamples)108 void WriteDumpFileHelper(const int16_t* aInput, size_t aSamples) { 109 mozilla::Unused << fwrite(aInput, sizeof(int16_t), aSamples, mFile); 110 fflush(mFile); 111 } 112 WriteDumpFileHelper(const float * aInput,size_t aSamples)113 void WriteDumpFileHelper(const float* aInput, size_t aSamples) { 114 using namespace mozilla; 115 116 AutoTArray<uint8_t, 1024 * 2> buf; 117 mozilla::ByteWriter<mozilla::LittleEndian> writer(buf); 118 for (uint32_t i = 0; i < aSamples; ++i) { 119 mozilla::DebugOnly<bool> rv = 120 writer.WriteU16(int16_t(aInput[i] * 32767.0f)); 121 MOZ_ASSERT(rv); 122 } 123 mozilla::Unused << fwrite(buf.Elements(), buf.Length(), 1, mFile); 124 fflush(mFile); 125 } 126 127 FILE* mFile = nullptr; 128 }; 129 130 #endif // WavDumper_h_ 131