| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #ifndef MEDIA_AUDIO_SOUNDS_WAV_AUDIO_HANDLER_H_ | 5 #ifndef MEDIA_AUDIO_SOUNDS_WAV_AUDIO_HANDLER_H_ |
| 6 #define MEDIA_AUDIO_SOUNDS_WAV_AUDIO_HANDLER_H_ | 6 #define MEDIA_AUDIO_SOUNDS_WAV_AUDIO_HANDLER_H_ |
| 7 | 7 |
| 8 #include "base/macros.h" |
| 9 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
| 9 #include "base/time/time.h" | 11 #include "base/time/time.h" |
| 10 #include "media/base/media_export.h" | 12 #include "media/base/media_export.h" |
| 11 | 13 |
| 12 namespace media { | 14 namespace media { |
| 13 | 15 |
| 14 class AudioBus; | 16 class AudioBus; |
| 15 | 17 |
| 16 // This class provides the input from wav file format. See | 18 // This class provides the input from wav file format. See |
| 17 // https://ccrma.stanford.edu/courses/422/projects/WaveFormat/ | 19 // https://ccrma.stanford.edu/courses/422/projects/WaveFormat/ |
| 18 class MEDIA_EXPORT WavAudioHandler { | 20 class MEDIA_EXPORT WavAudioHandler { |
| 19 public: | 21 public: |
| 20 explicit WavAudioHandler(const base::StringPiece& wav_data); | |
| 21 virtual ~WavAudioHandler(); | 22 virtual ~WavAudioHandler(); |
| 22 | 23 |
| 24 // Create a WavAudioHandler using |wav_data|. If |wav_data| cannot be parsed |
| 25 // correctly, the returned scoped_ptr will be null. The underlying memory for |
| 26 // wav_data must survive for the lifetime of this class. |
| 27 static scoped_ptr<WavAudioHandler> Create(const base::StringPiece wav_data); |
| 28 |
| 23 // Returns true when cursor points to the end of the track. | 29 // Returns true when cursor points to the end of the track. |
| 24 bool AtEnd(size_t cursor) const; | 30 bool AtEnd(size_t cursor) const; |
| 25 | 31 |
| 26 // Copies the audio data to |bus| starting from the |cursor| and in | 32 // Copies the audio data to |bus| starting from the |cursor| and in |
| 27 // the case of success stores the number of written bytes in | 33 // the case of success stores the number of written bytes in |
| 28 // |bytes_written|. |bytes_written| should not be NULL. | 34 // |bytes_written|. |bytes_written| should not be NULL. Returns false if the |
| 35 // operation was unsuccessful. Returns true otherwise. |
| 29 bool CopyTo(AudioBus* bus, size_t cursor, size_t* bytes_written) const; | 36 bool CopyTo(AudioBus* bus, size_t cursor, size_t* bytes_written) const; |
| 30 | 37 |
| 31 // Accessors. | 38 // Accessors. |
| 32 const base::StringPiece& data() const { return data_; } | 39 const base::StringPiece& data() const { return data_; } |
| 33 uint16_t num_channels() const { return num_channels_; } | 40 uint16_t num_channels() const { return num_channels_; } |
| 34 uint32_t sample_rate() const { return sample_rate_; } | 41 uint32_t sample_rate() const { return sample_rate_; } |
| 35 uint16_t bits_per_sample() const { return bits_per_sample_; } | 42 uint16_t bits_per_sample() const { return bits_per_sample_; } |
| 36 uint32_t total_frames() const { return total_frames_; } | 43 uint32_t total_frames() const { return total_frames_; } |
| 37 | 44 |
| 38 // Returns the duration of the entire audio chunk. | 45 // Returns the duration of the entire audio chunk. |
| 39 base::TimeDelta GetDuration() const; | 46 base::TimeDelta GetDuration() const; |
| 40 | 47 |
| 41 private: | 48 private: |
| 42 // Parses a chunk of wav format data. Returns the length of the chunk. | 49 // Note: It is preferred to pass |audio_data| by value here. |
| 43 int ParseSubChunk(const base::StringPiece& data); | 50 WavAudioHandler(base::StringPiece audio_data, |
| 44 | 51 uint16_t num_channels, |
| 45 // Parses the 'fmt' section chunk and stores |params_|. | 52 uint32_t sample_rate, |
| 46 bool ParseFmtChunk(const base::StringPiece& data); | 53 uint16_t bits_per_sample); |
| 47 | |
| 48 // Parses the 'data' section chunk and stores |data_|. | |
| 49 bool ParseDataChunk(const base::StringPiece& data); | |
| 50 | 54 |
| 51 // Data part of the |wav_data_|. | 55 // Data part of the |wav_data_|. |
| 52 base::StringPiece data_; | 56 const base::StringPiece data_; |
| 57 const uint16_t num_channels_; |
| 58 const uint32_t sample_rate_; |
| 59 const uint16_t bits_per_sample_; |
| 60 uint32_t total_frames_; |
| 53 | 61 |
| 54 uint16_t num_channels_; | 62 DISALLOW_COPY_AND_ASSIGN(WavAudioHandler); |
| 55 uint32_t sample_rate_; | |
| 56 uint16_t bits_per_sample_; | |
| 57 uint32_t total_frames_; | |
| 58 }; | 63 }; |
| 59 | 64 |
| 60 } // namespace media | 65 } // namespace media |
| 61 | 66 |
| 62 #endif // MEDIA_AUDIO_SOUNDS_WAV_AUDIO_HANDLER_H_ | 67 #endif // MEDIA_AUDIO_SOUNDS_WAV_AUDIO_HANDLER_H_ |
| OLD | NEW |