| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This application is a test for AudioRendererAlgorithmOLA. It reads in a | 5 // This application is a test for AudioRendererAlgorithmOLA. It reads in a |
| 6 // specified wav file (so far only 8, 16 and 32 bit are supported) and uses | 6 // specified wav file (so far only 8, 16 and 32 bit are supported) and uses |
| 7 // ARAO to scale the playback by a specified rate. Then it outputs the result | 7 // ARAO to scale the playback by a specified rate. Then it outputs the result |
| 8 // to the specified location. Command line calls should be as follows: | 8 // to the specified location. Command line calls should be as follows: |
| 9 // | 9 // |
| 10 // wav_ola_test RATE INFILE OUTFILE | 10 // wav_ola_test RATE INFILE OUTFILE |
| 11 | 11 |
| 12 #include <iostream> | 12 #include <iostream> |
| 13 #include <string> | 13 #include <string> |
| 14 | 14 |
| 15 #include "base/command_line.h" | 15 #include "base/command_line.h" |
| 16 #include "base/file_util.h" | 16 #include "base/file_util.h" |
| 17 #include "base/ref_counted.h" | 17 #include "base/ref_counted.h" |
| 18 #include "base/string_util.h" | 18 #include "base/string_util.h" |
| 19 #include "media/base/data_buffer.h" | 19 #include "media/base/data_buffer.h" |
| 20 #include "media/filters/audio_renderer_algorithm_ola.h" | 20 #include "media/filters/audio_renderer_algorithm_ola.h" |
| 21 | 21 |
| 22 using file_util::ScopedFILE; | 22 using file_util::ScopedFILE; |
| 23 using media::AudioRendererAlgorithmOLA; | 23 using media::AudioRendererAlgorithmOLA; |
| 24 using media::DataBuffer; | 24 using media::DataBuffer; |
| 25 | 25 |
| 26 const size_t kDefaultWindowSize = 4096; | 26 const double kDefaultWindowLength = 0.08; |
| 27 | 27 |
| 28 struct WavHeader { | 28 struct WavHeader { |
| 29 int32 riff; | 29 int32 riff; |
| 30 int32 chunk_size; | 30 int32 chunk_size; |
| 31 char unused0[8]; | 31 char unused0[8]; |
| 32 int32 subchunk1_size; | 32 int32 subchunk1_size; |
| 33 int16 audio_format; | 33 int16 audio_format; |
| 34 int16 channels; | 34 int16 channels; |
| 35 int32 sample_rate; | 35 int32 sample_rate; |
| 36 char unused1[6]; | 36 char unused1[6]; |
| 37 int16 bit_rate; | 37 int16 bit_rate; |
| 38 char unused2[4]; | 38 char unused2[4]; |
| 39 int32 subchunk2_size; | 39 int32 subchunk2_size; |
| 40 }; | 40 }; |
| 41 | 41 |
| 42 // Dummy class to feed data to OLA algorithm. Necessary to create callback. | 42 // Dummy class to feed data to OLA algorithm. Necessary to create callback. |
| 43 class Dummy { | 43 class Dummy { |
| 44 public: | 44 public: |
| 45 Dummy(FILE* in, AudioRendererAlgorithmOLA* ola) | 45 Dummy(FILE* in, AudioRendererAlgorithmOLA* ola, size_t window_size) |
| 46 : input_(in), | 46 : input_(in), |
| 47 ola_(ola) { | 47 ola_(ola), |
| 48 window_size_(window_size) { |
| 48 } | 49 } |
| 49 | 50 |
| 50 void ReadDataForAlg() { | 51 void ReadDataForAlg() { |
| 51 scoped_refptr<DataBuffer> b(new DataBuffer()); | 52 scoped_refptr<DataBuffer> b(new DataBuffer()); |
| 52 uint8* buf = b->GetWritableData(kDefaultWindowSize); | 53 uint8* buf = b->GetWritableData(window_size_); |
| 53 if (fread(buf, 1, kDefaultWindowSize, input_) > 0) { | 54 if (fread(buf, 1, window_size_, input_) > 0) { |
| 54 ola_->EnqueueBuffer(b.get()); | 55 ola_->EnqueueBuffer(b.get()); |
| 55 } | 56 } |
| 56 } | 57 } |
| 57 | 58 |
| 58 private: | 59 private: |
| 59 FILE* input_; | 60 FILE* input_; |
| 60 AudioRendererAlgorithmOLA* ola_; | 61 AudioRendererAlgorithmOLA* ola_; |
| 62 size_t window_size_; |
| 61 | 63 |
| 62 DISALLOW_COPY_AND_ASSIGN(Dummy); | 64 DISALLOW_COPY_AND_ASSIGN(Dummy); |
| 63 }; | 65 }; |
| 64 | 66 |
| 65 int main(int argc, const char** argv) { | 67 int main(int argc, const char** argv) { |
| 66 AudioRendererAlgorithmOLA ola; | 68 AudioRendererAlgorithmOLA ola; |
| 67 CommandLine::Init(argc, argv); | 69 CommandLine::Init(argc, argv); |
| 68 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); | 70 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); |
| 69 | 71 |
| 70 std::vector<std::wstring> filenames(cmd_line->GetLooseValues()); | 72 std::vector<std::wstring> filenames(cmd_line->GetLooseValues()); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 97 return 1; | 99 return 1; |
| 98 } | 100 } |
| 99 | 101 |
| 100 // Read in header. | 102 // Read in header. |
| 101 WavHeader wav; | 103 WavHeader wav; |
| 102 if (fread(&wav, sizeof(wav), 1, input.get()) < 1) { | 104 if (fread(&wav, sizeof(wav), 1, input.get()) < 1) { |
| 103 LOG(ERROR) << "could not read WAV header"; | 105 LOG(ERROR) << "could not read WAV header"; |
| 104 return 1; | 106 return 1; |
| 105 } | 107 } |
| 106 | 108 |
| 109 size_t window_size = static_cast<size_t>(wav.sample_rate |
| 110 * (wav.bit_rate / 8) |
| 111 * wav.channels |
| 112 * kDefaultWindowLength); |
| 113 |
| 107 // Instantiate dummy class and callback to feed data to |ola|. | 114 // Instantiate dummy class and callback to feed data to |ola|. |
| 108 Dummy guy(input.get(), &ola); | 115 Dummy guy(input.get(), &ola, window_size); |
| 109 AudioRendererAlgorithmOLA::RequestReadCallback* cb = | 116 AudioRendererAlgorithmOLA::RequestReadCallback* cb = |
| 110 NewCallback(&guy, &Dummy::ReadDataForAlg); | 117 NewCallback(&guy, &Dummy::ReadDataForAlg); |
| 111 ola.Initialize(wav.channels, | 118 ola.Initialize(wav.channels, |
| 119 wav.sample_rate, |
| 112 wav.bit_rate, | 120 wav.bit_rate, |
| 113 static_cast<float>(playback_rate), | 121 static_cast<float>(playback_rate), |
| 114 cb); | 122 cb); |
| 115 | 123 |
| 116 // Print out input format. | 124 // Print out input format. |
| 117 std::cout << in_path << "\n" | 125 std::cout << in_path << "\n" |
| 118 << "Channels: " << wav.channels << "\n" | 126 << "Channels: " << wav.channels << "\n" |
| 119 << "Sample Rate: " << wav.sample_rate << "\n" | 127 << "Sample Rate: " << wav.sample_rate << "\n" |
| 120 << "Bit Rate: " << wav.bit_rate << "\n" | 128 << "Bit Rate: " << wav.bit_rate << "\n" |
| 121 << "\n" | 129 << "\n" |
| 122 << "Scaling audio by " << playback_rate << "x..." << std::endl; | 130 << "Scaling audio by " << playback_rate << "x..." << std::endl; |
| 123 | 131 |
| 124 // Write the header back out again. | 132 // Write the header back out again. |
| 125 if (fwrite(&wav, sizeof(wav), 1, output.get()) < 1) { | 133 if (fwrite(&wav, sizeof(wav), 1, output.get()) < 1) { |
| 126 LOG(ERROR) << "could not write WAV header"; | 134 LOG(ERROR) << "could not write WAV header"; |
| 127 return 1; | 135 return 1; |
| 128 } | 136 } |
| 129 | 137 |
| 130 // Create buffer to be filled by |ola|. | 138 // Create buffer to be filled by |ola|. |
| 131 scoped_refptr<DataBuffer> buffer(new DataBuffer()); | 139 scoped_refptr<DataBuffer> buffer(new DataBuffer()); |
| 132 uint8* buf = buffer->GetWritableData(kDefaultWindowSize); | 140 uint8* buf = buffer->GetWritableData(window_size); |
| 133 | 141 |
| 134 // Keep track of bytes written to disk and bytes copied to |b|. | 142 // Keep track of bytes written to disk and bytes copied to |b|. |
| 135 size_t bytes_written = 0; | 143 size_t bytes_written = 0; |
| 136 size_t bytes; | 144 size_t bytes; |
| 137 while ((bytes = ola.FillBuffer(buffer.get())) > 0) { | 145 while ((bytes = ola.FillBuffer(buffer.get())) > 0) { |
| 138 if (fwrite(buf, 1, bytes, output.get()) != bytes) { | 146 if (fwrite(buf, 1, bytes, output.get()) != bytes) { |
| 139 LOG(ERROR) << "could not write data after " << bytes_written; | 147 LOG(ERROR) << "could not write data after " << bytes_written; |
| 140 } else { | 148 } else { |
| 141 bytes_written += bytes; | 149 bytes_written += bytes; |
| 142 } | 150 } |
| 143 } | 151 } |
| 144 | 152 |
| 145 // Seek back to the beginning of our output file and update the header. | 153 // Seek back to the beginning of our output file and update the header. |
| 146 wav.chunk_size = 36 + bytes_written; | 154 wav.chunk_size = 36 + bytes_written; |
| 147 wav.subchunk1_size = 16; | 155 wav.subchunk1_size = 16; |
| 148 wav.subchunk2_size = bytes_written; | 156 wav.subchunk2_size = bytes_written; |
| 149 fseek(output.get(), 0, SEEK_SET); | 157 fseek(output.get(), 0, SEEK_SET); |
| 150 if (fwrite(&wav, sizeof(wav), 1, output.get()) < 1) { | 158 if (fwrite(&wav, sizeof(wav), 1, output.get()) < 1) { |
| 151 LOG(ERROR) << "could not write wav header."; | 159 LOG(ERROR) << "could not write wav header."; |
| 152 return 1; | 160 return 1; |
| 153 } | 161 } |
| 154 | 162 |
| 155 return 0; | 163 return 0; |
| 156 } | 164 } |
| OLD | NEW |