OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 "webkit/glue/media/audio_decoder.h" |
| 6 |
| 7 #include <vector> |
| 8 #include "base/basictypes.h" |
| 9 #include "base/string_util.h" |
| 10 #include "media/filters/audio_file_reader.h" |
| 11 #include "third_party/WebKit/WebKit/chromium/public/WebAudioBus.h" |
| 12 |
| 13 using media::InMemoryAudioFileReader; |
| 14 using std::vector; |
| 15 using WebKit::WebAudioBus; |
| 16 |
| 17 namespace webkit_glue { |
| 18 |
| 19 // Decode in-memory audio file data. |
| 20 bool DecodeAudioFileData(WebKit::WebAudioBus* destination_bus, |
| 21 const char* data, size_t data_size, double sample_rate) { |
| 22 DCHECK(destination_bus); |
| 23 if (!destination_bus) |
| 24 return false; |
| 25 |
| 26 // Uses the FFMPEG library for audio file reading. |
| 27 InMemoryAudioFileReader reader(data, data_size); |
| 28 |
| 29 int result = reader.Open(); |
| 30 if (result) |
| 31 return false; |
| 32 |
| 33 unsigned number_of_channels = reader.NumberOfChannels(); |
| 34 double file_sample_rate = reader.GetSampleRate(); |
| 35 double duration = reader.GetDuration(); |
| 36 size_t number_of_frames = reader.Length(); |
| 37 |
| 38 // TODO(crogers) : do sample-rate conversion with FFMPEG. |
| 39 // For now, we're ignoring the requested 'sample_rate' and returning |
| 40 // the WebAudioBus at the file's sample-rate. |
| 41 // double destination_sample_rate = |
| 42 // (sample_rate != 0.0) ? sample_rate : file_sample_rate; |
| 43 double destination_sample_rate = file_sample_rate; |
| 44 |
| 45 DLOG(INFO) << "Decoding file data -" |
| 46 << " data: " << data |
| 47 << " data size: " << data_size |
| 48 << " duration: " << duration |
| 49 << " number of frames: " << number_of_frames |
| 50 << " sample rate: " << file_sample_rate |
| 51 << " number of channels: " << number_of_channels; |
| 52 |
| 53 // Change to destination sample-rate. |
| 54 number_of_frames *= (destination_sample_rate / file_sample_rate); |
| 55 |
| 56 // Allocate and configure the output audio channel data. |
| 57 destination_bus->initialize(number_of_channels, |
| 58 number_of_frames, |
| 59 destination_sample_rate); |
| 60 |
| 61 // Wrap the channel pointers which will receive the decoded PCM audio. |
| 62 vector<float*> audio_data; |
| 63 audio_data.reserve(number_of_channels); |
| 64 for (unsigned i = 0; i < number_of_channels; ++i) { |
| 65 audio_data.push_back(destination_bus->channelData(i)); |
| 66 } |
| 67 |
| 68 // Decode the audio file data. |
| 69 result = reader.Read(audio_data, number_of_frames); |
| 70 |
| 71 return result == 0; |
| 72 } |
| 73 |
| 74 } // namespace webkit_glue |
OLD | NEW |