Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(418)

Side by Side Diff: chrome/browser/speech/audio_encoder.cc

Issue 6294005: A quick fix for Bug 69906.... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 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 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 "chrome/browser/speech/audio_encoder.h" 5 #include "chrome/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/scoped_ptr.h" 9 #include "base/scoped_ptr.h"
10 #include "base/stl_util-inl.h" 10 #include "base/stl_util-inl.h"
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
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_input::AudioEncoder {
111 public: 111 public:
112 SpeexEncoder(int sampling_rate); 112 explicit SpeexEncoder(int sampling_rate);
113 virtual ~SpeexEncoder();
113 virtual void Encode(const short* samples, int num_samples); 114 virtual void Encode(const short* samples, int num_samples);
114 virtual void Flush() {} 115 virtual void Flush() {}
115 116
116 private: 117 private:
117 void* encoder_state_; 118 void* encoder_state_;
118 SpeexBits bits_; 119 SpeexBits bits_;
119 int samples_per_frame_; 120 int samples_per_frame_;
120 char encoded_frame_data_[kMaxSpeexFrameLength + 1]; // +1 for the frame size. 121 char encoded_frame_data_[kMaxSpeexFrameLength + 1]; // +1 for the frame size.
121 DISALLOW_COPY_AND_ASSIGN(SpeexEncoder); 122 DISALLOW_COPY_AND_ASSIGN(SpeexEncoder);
122 }; 123 };
123 124
124 SpeexEncoder::SpeexEncoder(int sampling_rate) 125 SpeexEncoder::SpeexEncoder(int sampling_rate)
125 : AudioEncoder(std::string(kContentTypeSpeex) + 126 : AudioEncoder(std::string(kContentTypeSpeex) +
126 base::IntToString(sampling_rate)) { 127 base::IntToString(sampling_rate)) {
127 // speex_bits_init() does not initialize all of the |bits_| struct. 128 // speex_bits_init() does not initialize all of the |bits_| struct.
128 memset(&bits_, 0, sizeof(bits_)); 129 memset(&bits_, 0, sizeof(bits_));
129 speex_bits_init(&bits_); 130 speex_bits_init(&bits_);
130 encoder_state_ = speex_encoder_init(&speex_wb_mode); 131 encoder_state_ = speex_encoder_init(&speex_wb_mode);
131 DCHECK(encoder_state_); 132 DCHECK(encoder_state_);
132 speex_encoder_ctl(encoder_state_, SPEEX_GET_FRAME_SIZE, &samples_per_frame_); 133 speex_encoder_ctl(encoder_state_, SPEEX_GET_FRAME_SIZE, &samples_per_frame_);
133 DCHECK(samples_per_frame_ > 0); 134 DCHECK(samples_per_frame_ > 0);
134 int quality = kSpeexEncodingQuality; 135 int quality = kSpeexEncodingQuality;
135 speex_encoder_ctl(encoder_state_, SPEEX_SET_QUALITY, &quality); 136 speex_encoder_ctl(encoder_state_, SPEEX_SET_QUALITY, &quality);
136 int vbr = 1; 137 int vbr = 1;
137 speex_encoder_ctl(encoder_state_, SPEEX_SET_VBR, &vbr); 138 speex_encoder_ctl(encoder_state_, SPEEX_SET_VBR, &vbr);
138 memset(encoded_frame_data_, 0, sizeof(encoded_frame_data_)); 139 memset(encoded_frame_data_, 0, sizeof(encoded_frame_data_));
139 } 140 }
140 141
142 SpeexEncoder::~SpeexEncoder() {
143 speex_bits_destroy(&bits_);
144 speex_encoder_destroy(encoder_state_);
145 }
146
141 void SpeexEncoder::Encode(const short* samples, int num_samples) { 147 void SpeexEncoder::Encode(const short* samples, int num_samples) {
142 // Drop incomplete frames, typically those which come in when recording stops. 148 // Drop incomplete frames, typically those which come in when recording stops.
143 num_samples -= (num_samples % samples_per_frame_); 149 num_samples -= (num_samples % samples_per_frame_);
144 for (int i = 0; i < num_samples; i += samples_per_frame_) { 150 for (int i = 0; i < num_samples; i += samples_per_frame_) {
145 speex_bits_reset(&bits_); 151 speex_bits_reset(&bits_);
146 speex_encode_int(encoder_state_, const_cast<spx_int16_t*>(samples + i), 152 speex_encode_int(encoder_state_, const_cast<spx_int16_t*>(samples + i),
147 &bits_); 153 &bits_);
148 154
149 // Encode the frame and place the size of the frame as the first byte. This 155 // Encode the frame and place the size of the frame as the first byte. This
150 // 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.
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 } 197 }
192 198
193 return true; 199 return true;
194 } 200 }
195 201
196 void AudioEncoder::AppendToBuffer(std::string* item) { 202 void AudioEncoder::AppendToBuffer(std::string* item) {
197 audio_buffers_.push_back(item); 203 audio_buffers_.push_back(item);
198 } 204 }
199 205
200 } // namespace speech_input 206 } // namespace speech_input
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698