1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/audio_modem/audio_recorder_impl.h"
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/logging.h"
13 #include "base/run_loop.h"
14 #include "base/synchronization/waitable_event.h"
15 #include "components/audio_modem/public/audio_modem_types.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "media/audio/audio_manager.h"
18 #include "media/audio/audio_manager_base.h"
19 #include "media/base/audio_bus.h"
21 namespace audio_modem
{
25 const float kProcessIntervalMs
= 500.0f
; // milliseconds.
27 void AudioBusToString(scoped_ptr
<media::AudioBus
> source
, std::string
* buffer
) {
28 buffer
->resize(source
->frames() * source
->channels() * sizeof(float));
29 float* buffer_view
= reinterpret_cast<float*>(string_as_array(buffer
));
31 const int channels
= source
->channels();
32 for (int ch
= 0; ch
< channels
; ++ch
) {
33 for (int si
= 0, di
= ch
; si
< source
->frames(); ++si
, di
+= channels
)
34 buffer_view
[di
] = source
->channel(ch
)[si
];
38 // Called every kProcessIntervalMs to process the recorded audio. This
39 // converts our samples to the required sample rate, interleaves the samples
40 // and sends them to the whispernet decoder to process.
42 scoped_ptr
<media::AudioBus
> bus
,
43 const AudioRecorderImpl::RecordedSamplesCallback
& callback
) {
45 AudioBusToString(bus
.Pass(), &samples
);
46 content::BrowserThread::PostTask(
47 content::BrowserThread::UI
, FROM_HERE
, base::Bind(callback
, samples
));
54 AudioRecorderImpl::AudioRecorderImpl()
55 : is_recording_(false),
57 total_buffer_frames_(0),
58 buffer_frame_index_(0) {
61 void AudioRecorderImpl::Initialize(
62 const RecordedSamplesCallback
& decode_callback
) {
63 decode_callback_
= decode_callback
;
64 media::AudioManager::Get()->GetTaskRunner()->PostTask(
66 base::Bind(&AudioRecorderImpl::InitializeOnAudioThread
,
67 base::Unretained(this)));
70 AudioRecorderImpl::~AudioRecorderImpl() {
73 void AudioRecorderImpl::Record() {
74 media::AudioManager::Get()->GetTaskRunner()->PostTask(
76 base::Bind(&AudioRecorderImpl::RecordOnAudioThread
,
77 base::Unretained(this)));
80 void AudioRecorderImpl::Stop() {
81 media::AudioManager::Get()->GetTaskRunner()->PostTask(
83 base::Bind(&AudioRecorderImpl::StopOnAudioThread
,
84 base::Unretained(this)));
87 void AudioRecorderImpl::Finalize() {
88 media::AudioManager::Get()->GetTaskRunner()->PostTask(
90 base::Bind(&AudioRecorderImpl::FinalizeOnAudioThread
,
91 base::Unretained(this)));
96 void AudioRecorderImpl::InitializeOnAudioThread() {
97 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread());
99 media::AudioParameters params
=
101 ? *params_for_testing_
102 : media::AudioManager::Get()->GetInputStreamParameters(
103 media::AudioManagerBase::kDefaultDeviceId
);
105 total_buffer_frames_
= kProcessIntervalMs
* params
.sample_rate() / 1000;
106 buffer_
= media::AudioBus::Create(params
.channels(), total_buffer_frames_
);
107 buffer_frame_index_
= 0;
109 stream_
= input_stream_for_testing_
110 ? input_stream_for_testing_
.get()
111 : media::AudioManager::Get()->MakeAudioInputStream(
112 params
, media::AudioManagerBase::kDefaultDeviceId
);
114 if (!stream_
|| !stream_
->Open()) {
115 LOG(ERROR
) << "Failed to open an input stream.";
122 stream_
->SetVolume(stream_
->GetMaxVolume());
125 void AudioRecorderImpl::RecordOnAudioThread() {
126 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread());
127 if (!stream_
|| is_recording_
)
130 VLOG(3) << "Starting recording.";
131 stream_
->Start(this);
132 is_recording_
= true;
135 void AudioRecorderImpl::StopOnAudioThread() {
136 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread());
137 if (!stream_
|| !is_recording_
)
140 VLOG(3) << "Stopping recording.";
142 is_recording_
= false;
145 void AudioRecorderImpl::StopAndCloseOnAudioThread() {
146 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread());
155 void AudioRecorderImpl::FinalizeOnAudioThread() {
156 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread());
157 StopAndCloseOnAudioThread();
161 void AudioRecorderImpl::OnData(media::AudioInputStream
* stream
,
162 const media::AudioBus
* source
,
163 uint32
/* hardware_delay_bytes */,
164 double /* volume */) {
165 // source->frames() == source_params.frames_per_buffer(), so we only have
166 // one chunk of data in the source; correspondingly set the destination
167 // size to one chunk.
169 int remaining_buffer_frames
= buffer_
->frames() - buffer_frame_index_
;
170 int frames_to_copy
= std::min(remaining_buffer_frames
, source
->frames());
171 source
->CopyPartialFramesTo(0, frames_to_copy
, buffer_frame_index_
,
173 buffer_frame_index_
+= frames_to_copy
;
175 // Buffer full, send it for processing.
176 if (buffer_
->frames() == buffer_frame_index_
) {
177 ProcessSamples(buffer_
.Pass(), decode_callback_
);
178 buffer_
= media::AudioBus::Create(source
->channels(), total_buffer_frames_
);
179 buffer_frame_index_
= 0;
181 // Copy any remaining frames in the source to our buffer.
182 int remaining_source_frames
= source
->frames() - frames_to_copy
;
183 source
->CopyPartialFramesTo(frames_to_copy
, remaining_source_frames
,
184 buffer_frame_index_
, buffer_
.get());
185 buffer_frame_index_
+= remaining_source_frames
;
189 void AudioRecorderImpl::OnError(media::AudioInputStream
* /* stream */) {
190 LOG(ERROR
) << "Error during sound recording.";
191 media::AudioManager::Get()->GetTaskRunner()->PostTask(
193 base::Bind(&AudioRecorderImpl::StopAndCloseOnAudioThread
,
194 base::Unretained(this)));
197 void AudioRecorderImpl::FlushAudioLoopForTesting() {
198 if (media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread())
201 // Queue task on the audio thread, when it is executed, that means we've
202 // successfully executed all the tasks before us.
204 media::AudioManager::Get()->GetTaskRunner()->PostTaskAndReply(
207 base::IgnoreResult(&AudioRecorderImpl::FlushAudioLoopForTesting
),
208 base::Unretained(this)),
213 } // namespace audio_modem