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 <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
| 9 #include <memory> |
| 10 |
9 #include "base/logging.h" | 11 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
12 #include "content/browser/speech/audio_buffer.h" | 13 #include "content/browser/speech/audio_buffer.h" |
13 | 14 |
14 namespace content { | 15 namespace content { |
15 | 16 |
16 namespace { | 17 namespace { |
17 | 18 |
18 const char* const kContentTypeFLAC = "audio/x-flac; rate="; | 19 const char* const kContentTypeFLAC = "audio/x-flac; rate="; |
19 const int kFLACCompressionLevel = 0; // 0 for speed | 20 const int kFLACCompressionLevel = 0; // 0 for speed |
20 | 21 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 if (!is_encoder_initialized_) { | 56 if (!is_encoder_initialized_) { |
56 const FLAC__StreamEncoderInitStatus encoder_status = | 57 const FLAC__StreamEncoderInitStatus encoder_status = |
57 FLAC__stream_encoder_init_stream(encoder_, WriteCallback, NULL, NULL, | 58 FLAC__stream_encoder_init_stream(encoder_, WriteCallback, NULL, NULL, |
58 NULL, &encoded_audio_buffer_); | 59 NULL, &encoded_audio_buffer_); |
59 DCHECK_EQ(encoder_status, FLAC__STREAM_ENCODER_INIT_STATUS_OK); | 60 DCHECK_EQ(encoder_status, FLAC__STREAM_ENCODER_INIT_STATUS_OK); |
60 is_encoder_initialized_ = true; | 61 is_encoder_initialized_ = true; |
61 } | 62 } |
62 | 63 |
63 // FLAC encoder wants samples as int32s. | 64 // FLAC encoder wants samples as int32s. |
64 const int num_samples = raw_audio.NumSamples(); | 65 const int num_samples = raw_audio.NumSamples(); |
65 scoped_ptr<FLAC__int32[]> flac_samples(new FLAC__int32[num_samples]); | 66 std::unique_ptr<FLAC__int32[]> flac_samples(new FLAC__int32[num_samples]); |
66 FLAC__int32* flac_samples_ptr = flac_samples.get(); | 67 FLAC__int32* flac_samples_ptr = flac_samples.get(); |
67 for (int i = 0; i < num_samples; ++i) | 68 for (int i = 0; i < num_samples; ++i) |
68 flac_samples_ptr[i] = static_cast<FLAC__int32>(raw_audio.GetSample16(i)); | 69 flac_samples_ptr[i] = static_cast<FLAC__int32>(raw_audio.GetSample16(i)); |
69 | 70 |
70 FLAC__stream_encoder_process(encoder_, &flac_samples_ptr, num_samples); | 71 FLAC__stream_encoder_process(encoder_, &flac_samples_ptr, num_samples); |
71 } | 72 } |
72 | 73 |
73 void AudioEncoder::Flush() { | 74 void AudioEncoder::Flush() { |
74 FLAC__stream_encoder_finish(encoder_); | 75 FLAC__stream_encoder_finish(encoder_); |
75 } | 76 } |
76 | 77 |
77 scoped_refptr<AudioChunk> AudioEncoder::GetEncodedDataAndClear() { | 78 scoped_refptr<AudioChunk> AudioEncoder::GetEncodedDataAndClear() { |
78 return encoded_audio_buffer_.DequeueAll(); | 79 return encoded_audio_buffer_.DequeueAll(); |
79 } | 80 } |
80 | 81 |
81 std::string AudioEncoder::GetMimeType() { | 82 std::string AudioEncoder::GetMimeType() { |
82 return std::string(kContentTypeFLAC) + | 83 return std::string(kContentTypeFLAC) + |
83 base::UintToString(FLAC__stream_encoder_get_sample_rate(encoder_)); | 84 base::UintToString(FLAC__stream_encoder_get_sample_rate(encoder_)); |
84 } | 85 } |
85 | 86 |
86 int AudioEncoder::GetBitsPerSample() { | 87 int AudioEncoder::GetBitsPerSample() { |
87 return FLAC__stream_encoder_get_bits_per_sample(encoder_); | 88 return FLAC__stream_encoder_get_bits_per_sample(encoder_); |
88 } | 89 } |
89 | 90 |
90 } // namespace content | 91 } // namespace content |
OLD | NEW |