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/strings/string_number_conversions.h" | 11 #include "base/strings/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/include/FLAC/stream_encoder.h" | 13 #include "third_party/flac/include/FLAC/stream_encoder.h" |
14 #include "third_party/speex/include/speex/speex.h" | |
15 | 14 |
16 namespace content { | 15 namespace content { |
17 namespace { | 16 namespace { |
18 | 17 |
19 //-------------------------------- FLACEncoder --------------------------------- | 18 //-------------------------------- FLACEncoder --------------------------------- |
20 | 19 |
21 const char* const kContentTypeFLAC = "audio/x-flac; rate="; | 20 const char* const kContentTypeFLAC = "audio/x-flac; rate="; |
22 const int kFLACCompressionLevel = 0; // 0 for speed | 21 const int kFLACCompressionLevel = 0; // 0 for speed |
23 | 22 |
24 class FLACEncoder : public AudioEncoder { | 23 class FLACEncoder : public AudioEncoder { |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 for (int i = 0; i < num_samples; ++i) | 92 for (int i = 0; i < num_samples; ++i) |
94 flac_samples_ptr[i] = static_cast<FLAC__int32>(raw_audio.GetSample16(i)); | 93 flac_samples_ptr[i] = static_cast<FLAC__int32>(raw_audio.GetSample16(i)); |
95 | 94 |
96 FLAC__stream_encoder_process(encoder_, &flac_samples_ptr, num_samples); | 95 FLAC__stream_encoder_process(encoder_, &flac_samples_ptr, num_samples); |
97 } | 96 } |
98 | 97 |
99 void FLACEncoder::Flush() { | 98 void FLACEncoder::Flush() { |
100 FLAC__stream_encoder_finish(encoder_); | 99 FLAC__stream_encoder_finish(encoder_); |
101 } | 100 } |
102 | 101 |
103 //-------------------------------- SpeexEncoder -------------------------------- | |
104 | |
105 const char* const kContentTypeSpeex = "audio/x-speex-with-header-byte; rate="; | |
106 const int kSpeexEncodingQuality = 8; | |
107 const int kMaxSpeexFrameLength = 110; // (44kbps rate sampled at 32kHz). | |
108 | |
109 // Since the frame length gets written out as a byte in the encoded packet, | |
110 // make sure it is within the byte range. | |
111 static_assert(kMaxSpeexFrameLength <= 0xFF, "invalid length"); | |
112 | |
113 class SpeexEncoder : public AudioEncoder { | |
114 public: | |
115 explicit SpeexEncoder(int sampling_rate, int bits_per_sample); | |
116 ~SpeexEncoder() override; | |
117 void Encode(const AudioChunk& raw_audio) override; | |
118 void Flush() override {} | |
119 | |
120 private: | |
121 void* encoder_state_; | |
122 SpeexBits bits_; | |
123 int samples_per_frame_; | |
124 char encoded_frame_data_[kMaxSpeexFrameLength + 1]; // +1 for the frame size. | |
125 DISALLOW_COPY_AND_ASSIGN(SpeexEncoder); | |
126 }; | |
127 | |
128 SpeexEncoder::SpeexEncoder(int sampling_rate, int bits_per_sample) | |
129 : AudioEncoder(std::string(kContentTypeSpeex) + | |
130 base::IntToString(sampling_rate), | |
131 bits_per_sample) { | |
132 // speex_bits_init() does not initialize all of the |bits_| struct. | |
133 memset(&bits_, 0, sizeof(bits_)); | |
134 speex_bits_init(&bits_); | |
135 encoder_state_ = speex_encoder_init(&speex_wb_mode); | |
136 DCHECK(encoder_state_); | |
137 speex_encoder_ctl(encoder_state_, SPEEX_GET_FRAME_SIZE, &samples_per_frame_); | |
138 DCHECK(samples_per_frame_ > 0); | |
139 int quality = kSpeexEncodingQuality; | |
140 speex_encoder_ctl(encoder_state_, SPEEX_SET_QUALITY, &quality); | |
141 int vbr = 1; | |
142 speex_encoder_ctl(encoder_state_, SPEEX_SET_VBR, &vbr); | |
143 memset(encoded_frame_data_, 0, sizeof(encoded_frame_data_)); | |
144 } | |
145 | |
146 SpeexEncoder::~SpeexEncoder() { | |
147 speex_bits_destroy(&bits_); | |
148 speex_encoder_destroy(encoder_state_); | |
149 } | |
150 | |
151 void SpeexEncoder::Encode(const AudioChunk& raw_audio) { | |
152 spx_int16_t* src_buffer = | |
153 const_cast<spx_int16_t*>(raw_audio.SamplesData16()); | |
154 int num_samples = raw_audio.NumSamples(); | |
155 // Drop incomplete frames, typically those which come in when recording stops. | |
156 num_samples -= (num_samples % samples_per_frame_); | |
157 for (int i = 0; i < num_samples; i += samples_per_frame_) { | |
158 speex_bits_reset(&bits_); | |
159 speex_encode_int(encoder_state_, src_buffer + i, &bits_); | |
160 | |
161 // Encode the frame and place the size of the frame as the first byte. This | |
162 // is the packet format for MIME type x-speex-with-header-byte. | |
163 int frame_length = speex_bits_write(&bits_, encoded_frame_data_ + 1, | |
164 kMaxSpeexFrameLength); | |
165 encoded_frame_data_[0] = static_cast<char>(frame_length); | |
166 encoded_audio_buffer_.Enqueue( | |
167 reinterpret_cast<uint8*>(&encoded_frame_data_[0]), frame_length + 1); | |
168 } | |
169 } | |
170 | |
171 } // namespace | 102 } // namespace |
172 | 103 |
173 AudioEncoder* AudioEncoder::Create(Codec codec, | 104 AudioEncoder* AudioEncoder::Create(int sampling_rate, int bits_per_sample) { |
174 int sampling_rate, | 105 return new FLACEncoder(sampling_rate, bits_per_sample); |
175 int bits_per_sample) { | |
176 if (codec == CODEC_FLAC) | |
177 return new FLACEncoder(sampling_rate, bits_per_sample); | |
178 return new SpeexEncoder(sampling_rate, bits_per_sample); | |
179 } | 106 } |
180 | 107 |
181 AudioEncoder::AudioEncoder(const std::string& mime_type, int bits_per_sample) | 108 AudioEncoder::AudioEncoder(const std::string& mime_type, int bits_per_sample) |
182 : encoded_audio_buffer_(1), /* Byte granularity of encoded samples. */ | 109 : encoded_audio_buffer_(1), /* Byte granularity of encoded samples. */ |
183 mime_type_(mime_type), | 110 mime_type_(mime_type), |
184 bits_per_sample_(bits_per_sample) { | 111 bits_per_sample_(bits_per_sample) { |
185 } | 112 } |
186 | 113 |
187 AudioEncoder::~AudioEncoder() { | 114 AudioEncoder::~AudioEncoder() { |
188 } | 115 } |
189 | 116 |
190 scoped_refptr<AudioChunk> AudioEncoder::GetEncodedDataAndClear() { | 117 scoped_refptr<AudioChunk> AudioEncoder::GetEncodedDataAndClear() { |
191 return encoded_audio_buffer_.DequeueAll(); | 118 return encoded_audio_buffer_.DequeueAll(); |
192 } | 119 } |
193 | 120 |
194 } // namespace content | 121 } // namespace content |
OLD | NEW |