| OLD | NEW |
| 1 // Copyright (c) 2011 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 "third_party/flac/flac.h" | 12 #include "third_party/flac/flac.h" |
| 13 #include "third_party/speex/speex.h" | 13 #include "third_party/speex/speex.h" |
| 14 | 14 |
| 15 using std::string; | 15 using std::string; |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 //-------------------------------- FLACEncoder --------------------------------- | 19 //-------------------------------- FLACEncoder --------------------------------- |
| 20 | 20 |
| 21 const char* const kContentTypeFLAC = "audio/x-flac; rate="; | 21 const char* const kContentTypeFLAC = "audio/x-flac; rate="; |
| 22 const int kFLACCompressionLevel = 0; // 0 for speed | 22 const int kFLACCompressionLevel = 0; // 0 for speed |
| 23 | 23 |
| 24 class FLACEncoder : public speech_input::AudioEncoder { | 24 class FLACEncoder : public speech::AudioEncoder { |
| 25 public: | 25 public: |
| 26 FLACEncoder(int sampling_rate, int bits_per_sample); | 26 FLACEncoder(int sampling_rate, int bits_per_sample); |
| 27 virtual ~FLACEncoder(); | 27 virtual ~FLACEncoder(); |
| 28 virtual void Encode(const short* samples, int num_samples); | 28 virtual void Encode(const short* samples, int num_samples); |
| 29 virtual void Flush(); | 29 virtual void Flush(); |
| 30 | 30 |
| 31 private: | 31 private: |
| 32 static FLAC__StreamEncoderWriteStatus WriteCallback( | 32 static FLAC__StreamEncoderWriteStatus WriteCallback( |
| 33 const FLAC__StreamEncoder* encoder, | 33 const FLAC__StreamEncoder* encoder, |
| 34 const FLAC__byte buffer[], | 34 const FLAC__byte buffer[], |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 //-------------------------------- SpeexEncoder -------------------------------- | 100 //-------------------------------- SpeexEncoder -------------------------------- |
| 101 | 101 |
| 102 const char* const kContentTypeSpeex = "audio/x-speex-with-header-byte; rate="; | 102 const char* const kContentTypeSpeex = "audio/x-speex-with-header-byte; rate="; |
| 103 const int kSpeexEncodingQuality = 8; | 103 const int kSpeexEncodingQuality = 8; |
| 104 const int kMaxSpeexFrameLength = 110; // (44kbps rate sampled at 32kHz). | 104 const int kMaxSpeexFrameLength = 110; // (44kbps rate sampled at 32kHz). |
| 105 | 105 |
| 106 // Since the frame length gets written out as a byte in the encoded packet, | 106 // Since the frame length gets written out as a byte in the encoded packet, |
| 107 // make sure it is within the byte range. | 107 // make sure it is within the byte range. |
| 108 COMPILE_ASSERT(kMaxSpeexFrameLength <= 0xFF, invalidLength); | 108 COMPILE_ASSERT(kMaxSpeexFrameLength <= 0xFF, invalidLength); |
| 109 | 109 |
| 110 class SpeexEncoder : public speech_input::AudioEncoder { | 110 class SpeexEncoder : public speech::AudioEncoder { |
| 111 public: | 111 public: |
| 112 explicit SpeexEncoder(int sampling_rate); | 112 explicit SpeexEncoder(int sampling_rate); |
| 113 virtual ~SpeexEncoder(); | 113 virtual ~SpeexEncoder(); |
| 114 virtual void Encode(const short* samples, int num_samples); | 114 virtual void Encode(const short* samples, int num_samples); |
| 115 virtual void Flush() {} | 115 virtual void Flush() {} |
| 116 | 116 |
| 117 private: | 117 private: |
| 118 void* encoder_state_; | 118 void* encoder_state_; |
| 119 SpeexBits bits_; | 119 SpeexBits bits_; |
| 120 int samples_per_frame_; | 120 int samples_per_frame_; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 // is the packet format for MIME type x-speex-with-header-byte. | 156 // is the packet format for MIME type x-speex-with-header-byte. |
| 157 int frame_length = speex_bits_write(&bits_, encoded_frame_data_ + 1, | 157 int frame_length = speex_bits_write(&bits_, encoded_frame_data_ + 1, |
| 158 kMaxSpeexFrameLength); | 158 kMaxSpeexFrameLength); |
| 159 encoded_frame_data_[0] = static_cast<char>(frame_length); | 159 encoded_frame_data_[0] = static_cast<char>(frame_length); |
| 160 AppendToBuffer(new string(encoded_frame_data_, frame_length + 1)); | 160 AppendToBuffer(new string(encoded_frame_data_, frame_length + 1)); |
| 161 } | 161 } |
| 162 } | 162 } |
| 163 | 163 |
| 164 } // namespace | 164 } // namespace |
| 165 | 165 |
| 166 namespace speech_input { | 166 namespace speech { |
| 167 | 167 |
| 168 AudioEncoder* AudioEncoder::Create(Codec codec, | 168 AudioEncoder* AudioEncoder::Create(Codec codec, |
| 169 int sampling_rate, | 169 int sampling_rate, |
| 170 int bits_per_sample) { | 170 int bits_per_sample) { |
| 171 if (codec == CODEC_FLAC) | 171 if (codec == CODEC_FLAC) |
| 172 return new FLACEncoder(sampling_rate, bits_per_sample); | 172 return new FLACEncoder(sampling_rate, bits_per_sample); |
| 173 return new SpeexEncoder(sampling_rate); | 173 return new SpeexEncoder(sampling_rate); |
| 174 } | 174 } |
| 175 | 175 |
| 176 AudioEncoder::AudioEncoder(const std::string& mime_type) | 176 AudioEncoder::AudioEncoder(const std::string& mime_type) |
| (...skipping 21 matching lines...) Expand all Loading... |
| 198 | 198 |
| 199 STLDeleteElements(&audio_buffers_); | 199 STLDeleteElements(&audio_buffers_); |
| 200 | 200 |
| 201 return true; | 201 return true; |
| 202 } | 202 } |
| 203 | 203 |
| 204 void AudioEncoder::AppendToBuffer(std::string* item) { | 204 void AudioEncoder::AppendToBuffer(std::string* item) { |
| 205 audio_buffers_.push_back(item); | 205 audio_buffers_.push_back(item); |
| 206 } | 206 } |
| 207 | 207 |
| 208 } // namespace speech_input | 208 } // namespace speech |
| OLD | NEW |