Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "webrtc_browsertest_audio.h" | |
| 6 | |
| 7 #include "base/files/file.h" | |
| 8 #include "base/files/file_path.h" | |
| 9 #include "media/audio/audio_power_monitor.h" | |
| 10 #include "media/audio/sounds/wav_audio_handler.h" | |
| 11 #include "media/base/audio_bus.h" | |
| 12 | |
| 13 namespace { | |
| 14 // Opens |wav_filename|, reads it and loads it as a wav file. This function will | |
| 15 // bluntly trigger CHECKs if we can't read the file or if it's malformed. The | |
| 16 // caller takes ownership of the returned data. The size of the data is stored | |
| 17 // in |read_length|. | |
| 18 scoped_ptr<uint8[]> ReadWavFile(const base::FilePath& wav_filename, | |
| 19 size_t* file_length) { | |
| 20 base::File wav_file( | |
| 21 wav_filename, base::File::FLAG_OPEN | base::File::FLAG_READ); | |
| 22 if (!wav_file.IsValid()) { | |
| 23 CHECK(false) << "Failed to read " << wav_filename.value(); | |
| 24 return nullptr; | |
| 25 } | |
| 26 | |
| 27 size_t wav_file_length = wav_file.GetLength(); | |
| 28 | |
| 29 uint8* wav_file_data = new uint8[wav_file_length]; | |
| 30 size_t read_bytes = wav_file.Read(0, reinterpret_cast<char*>(wav_file_data), | |
| 31 wav_file_length); | |
| 32 if (read_bytes != wav_file_length) { | |
| 33 CHECK(false) << "Failed to read all bytes of " << wav_filename.value(); | |
| 34 return nullptr; | |
| 35 } | |
| 36 *file_length = wav_file_length; | |
| 37 return scoped_ptr<uint8[]>(wav_file_data); | |
| 38 } | |
| 39 | |
| 40 scoped_ptr<media::WavAudioHandler> CreateWavAudioHandler( | |
| 41 const base::FilePath& wav_filename, const uint8* wav_file_data, | |
| 42 size_t wav_file_length) { | |
| 43 base::StringPiece wav_data(reinterpret_cast<const char*>(wav_file_data), | |
| 44 wav_file_length); | |
| 45 scoped_ptr<media::WavAudioHandler> wav_audio_handler( | |
| 46 new media::WavAudioHandler(wav_data)); | |
| 47 | |
| 48 return wav_audio_handler.Pass(); | |
| 49 } | |
| 50 | |
| 51 } // namespace | |
| 52 | |
| 53 namespace test { | |
| 54 | |
| 55 enum { kPowerMeasurementTimeConstantMillis = 10 }; | |
| 56 | |
| 57 std::vector<float> ComputeAudioEnergyForWavFile( | |
| 58 const base::FilePath& wav_filename) { | |
| 59 // Read the file, and put its data in a scoped_ptr so it gets deleted later. | |
| 60 size_t file_length = 0; | |
| 61 scoped_ptr<uint8[]> wav_file_data = ReadWavFile(wav_filename, &file_length); | |
| 62 scoped_ptr<media::WavAudioHandler> wav_audio_handler = CreateWavAudioHandler( | |
| 63 wav_filename, wav_file_data.get(), file_length); | |
| 64 | |
| 65 // Set up the bus to read 10 ms at a time. | |
|
bjornv
2014/12/18 11:30:27
Could we set up the bus to read the entire segment
| |
| 66 int num_frames_in_10_ms = | |
| 67 10 * wav_audio_handler->params().sample_rate() / 1000; | |
| 68 media::AudioParameters bus_params = media::AudioParameters( | |
| 69 wav_audio_handler->params().format(), | |
| 70 wav_audio_handler->params().channel_layout(), | |
| 71 wav_audio_handler->params().sample_rate(), | |
| 72 wav_audio_handler->params().bits_per_sample(), | |
| 73 num_frames_in_10_ms); | |
| 74 | |
| 75 scoped_ptr<media::AudioBus> audio_bus = | |
| 76 media::AudioBus::Create(bus_params); | |
| 77 media::AudioPowerMonitor power_monitor( | |
| 78 wav_audio_handler->params().sample_rate(), | |
| 79 base::TimeDelta::FromMilliseconds(kPowerMeasurementTimeConstantMillis)); | |
| 80 | |
| 81 int read_pos = 0; | |
| 82 std::vector<float> results; | |
| 83 | |
| 84 while (!wav_audio_handler->AtEnd(read_pos)) { | |
| 85 size_t bytes_written; | |
| 86 wav_audio_handler->CopyTo(audio_bus.get(), read_pos, &bytes_written); | |
| 87 power_monitor.Scan(*audio_bus, audio_bus->frames()); | |
| 88 results.push_back(power_monitor.ReadCurrentPowerAndClip().first); | |
| 89 read_pos += bytes_written; | |
| 90 } | |
| 91 | |
| 92 return results; | |
| 93 } | |
| 94 | |
| 95 } // namespace test | |
| OLD | NEW |