| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "content/browser/speech/audio_encoder.h" | 5 #include "content/browser/speech/audio_encoder.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
| 12 #include "content/browser/speech/audio_buffer.h" | 12 #include "content/browser/speech/audio_buffer.h" |
| 13 #include "third_party/flac/flac.h" | 13 #include "third_party/flac/flac.h" |
| 14 #include "third_party/speex/speex.h" | 14 #include "third_party/speex/speex.h" |
| 15 | 15 |
| 16 using std::string; | 16 namespace content { |
| 17 using speech::AudioChunk; | |
| 18 | |
| 19 namespace { | 17 namespace { |
| 20 | 18 |
| 21 //-------------------------------- FLACEncoder --------------------------------- | 19 //-------------------------------- FLACEncoder --------------------------------- |
| 22 | 20 |
| 23 const char* const kContentTypeFLAC = "audio/x-flac; rate="; | 21 const char* const kContentTypeFLAC = "audio/x-flac; rate="; |
| 24 const int kFLACCompressionLevel = 0; // 0 for speed | 22 const int kFLACCompressionLevel = 0; // 0 for speed |
| 25 | 23 |
| 26 class FLACEncoder : public speech::AudioEncoder { | 24 class FLACEncoder : public AudioEncoder { |
| 27 public: | 25 public: |
| 28 FLACEncoder(int sampling_rate, int bits_per_sample); | 26 FLACEncoder(int sampling_rate, int bits_per_sample); |
| 29 virtual ~FLACEncoder(); | 27 virtual ~FLACEncoder(); |
| 30 virtual void Encode(const AudioChunk& raw_audio) OVERRIDE; | 28 virtual void Encode(const AudioChunk& raw_audio) OVERRIDE; |
| 31 virtual void Flush() OVERRIDE; | 29 virtual void Flush() OVERRIDE; |
| 32 | 30 |
| 33 private: | 31 private: |
| 34 static FLAC__StreamEncoderWriteStatus WriteCallback( | 32 static FLAC__StreamEncoderWriteStatus WriteCallback( |
| 35 const FLAC__StreamEncoder* encoder, | 33 const FLAC__StreamEncoder* encoder, |
| 36 const FLAC__byte buffer[], | 34 const FLAC__byte buffer[], |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 //-------------------------------- SpeexEncoder -------------------------------- | 103 //-------------------------------- SpeexEncoder -------------------------------- |
| 106 | 104 |
| 107 const char* const kContentTypeSpeex = "audio/x-speex-with-header-byte; rate="; | 105 const char* const kContentTypeSpeex = "audio/x-speex-with-header-byte; rate="; |
| 108 const int kSpeexEncodingQuality = 8; | 106 const int kSpeexEncodingQuality = 8; |
| 109 const int kMaxSpeexFrameLength = 110; // (44kbps rate sampled at 32kHz). | 107 const int kMaxSpeexFrameLength = 110; // (44kbps rate sampled at 32kHz). |
| 110 | 108 |
| 111 // Since the frame length gets written out as a byte in the encoded packet, | 109 // Since the frame length gets written out as a byte in the encoded packet, |
| 112 // make sure it is within the byte range. | 110 // make sure it is within the byte range. |
| 113 COMPILE_ASSERT(kMaxSpeexFrameLength <= 0xFF, invalidLength); | 111 COMPILE_ASSERT(kMaxSpeexFrameLength <= 0xFF, invalidLength); |
| 114 | 112 |
| 115 class SpeexEncoder : public speech::AudioEncoder { | 113 class SpeexEncoder : public AudioEncoder { |
| 116 public: | 114 public: |
| 117 explicit SpeexEncoder(int sampling_rate, int bits_per_sample); | 115 explicit SpeexEncoder(int sampling_rate, int bits_per_sample); |
| 118 virtual ~SpeexEncoder(); | 116 virtual ~SpeexEncoder(); |
| 119 virtual void Encode(const AudioChunk& raw_audio) OVERRIDE; | 117 virtual void Encode(const AudioChunk& raw_audio) OVERRIDE; |
| 120 virtual void Flush() OVERRIDE {} | 118 virtual void Flush() OVERRIDE {} |
| 121 | 119 |
| 122 private: | 120 private: |
| 123 void* encoder_state_; | 121 void* encoder_state_; |
| 124 SpeexBits bits_; | 122 SpeexBits bits_; |
| 125 int samples_per_frame_; | 123 int samples_per_frame_; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 int frame_length = speex_bits_write(&bits_, encoded_frame_data_ + 1, | 163 int frame_length = speex_bits_write(&bits_, encoded_frame_data_ + 1, |
| 166 kMaxSpeexFrameLength); | 164 kMaxSpeexFrameLength); |
| 167 encoded_frame_data_[0] = static_cast<char>(frame_length); | 165 encoded_frame_data_[0] = static_cast<char>(frame_length); |
| 168 encoded_audio_buffer_.Enqueue( | 166 encoded_audio_buffer_.Enqueue( |
| 169 reinterpret_cast<uint8*>(&encoded_frame_data_[0]), frame_length + 1); | 167 reinterpret_cast<uint8*>(&encoded_frame_data_[0]), frame_length + 1); |
| 170 } | 168 } |
| 171 } | 169 } |
| 172 | 170 |
| 173 } // namespace | 171 } // namespace |
| 174 | 172 |
| 175 namespace speech { | |
| 176 | |
| 177 AudioEncoder* AudioEncoder::Create(Codec codec, | 173 AudioEncoder* AudioEncoder::Create(Codec codec, |
| 178 int sampling_rate, | 174 int sampling_rate, |
| 179 int bits_per_sample) { | 175 int bits_per_sample) { |
| 180 if (codec == CODEC_FLAC) | 176 if (codec == CODEC_FLAC) |
| 181 return new FLACEncoder(sampling_rate, bits_per_sample); | 177 return new FLACEncoder(sampling_rate, bits_per_sample); |
| 182 return new SpeexEncoder(sampling_rate, bits_per_sample); | 178 return new SpeexEncoder(sampling_rate, bits_per_sample); |
| 183 } | 179 } |
| 184 | 180 |
| 185 AudioEncoder::AudioEncoder(const std::string& mime_type, int bits_per_sample) | 181 AudioEncoder::AudioEncoder(const std::string& mime_type, int bits_per_sample) |
| 186 : encoded_audio_buffer_(1), /* Byte granularity of encoded samples. */ | 182 : encoded_audio_buffer_(1), /* Byte granularity of encoded samples. */ |
| 187 mime_type_(mime_type), | 183 mime_type_(mime_type), |
| 188 bits_per_sample_(bits_per_sample) { | 184 bits_per_sample_(bits_per_sample) { |
| 189 } | 185 } |
| 190 | 186 |
| 191 AudioEncoder::~AudioEncoder() { | 187 AudioEncoder::~AudioEncoder() { |
| 192 } | 188 } |
| 193 | 189 |
| 194 scoped_refptr<AudioChunk> AudioEncoder::GetEncodedDataAndClear() { | 190 scoped_refptr<AudioChunk> AudioEncoder::GetEncodedDataAndClear() { |
| 195 return encoded_audio_buffer_.DequeueAll(); | 191 return encoded_audio_buffer_.DequeueAll(); |
| 196 } | 192 } |
| 197 | 193 |
| 198 } // namespace speech | 194 } // namespace content |
| OLD | NEW |