Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / base / multi_channel_resampler.h
blob690f750bb122beb37c84574b9a6c00a11614c97c
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
3 // found in the LICENSE file.
5 #ifndef MEDIA_BASE_MULTI_CHANNEL_RESAMPLER_H_
6 #define MEDIA_BASE_MULTI_CHANNEL_RESAMPLER_H_
8 #include <vector>
10 #include "base/callback.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/scoped_vector.h"
13 #include "media/base/sinc_resampler.h"
15 namespace media {
16 class AudioBus;
18 // MultiChannelResampler is a multi channel wrapper for SincResampler; allowing
19 // high quality sample rate conversion of multiple channels at once.
20 class MEDIA_EXPORT MultiChannelResampler {
21 public:
22 // Callback type for providing more data into the resampler. Expects AudioBus
23 // to be completely filled with data upon return; zero padded if not enough
24 // frames are available to satisfy the request. |frame_delay| is the number
25 // of output frames already processed and can be used to estimate delay.
26 typedef base::Callback<void(int frame_delay, AudioBus* audio_bus)> ReadCB;
28 // Constructs a MultiChannelResampler with the specified |read_cb|, which is
29 // used to acquire audio data for resampling. |io_sample_rate_ratio| is the
30 // ratio of input / output sample rates. |request_frames| is the size in
31 // frames of the AudioBus to be filled by |read_cb|.
32 MultiChannelResampler(int channels,
33 double io_sample_rate_ratio,
34 size_t request_frames,
35 const ReadCB& read_cb);
36 virtual ~MultiChannelResampler();
38 // Resamples |frames| of data from |read_cb_| into AudioBus.
39 void Resample(int frames, AudioBus* audio_bus);
41 // Flush all buffered data and reset internal indices. Not thread safe, do
42 // not call while Resample() is in progress.
43 void Flush();
45 // Update ratio for all SincResamplers. SetRatio() will cause reconstruction
46 // of the kernels used for resampling. Not thread safe, do not call while
47 // Resample() is in progress.
48 void SetRatio(double io_sample_rate_ratio);
50 // The maximum size in frames that guarantees Resample() will only make a
51 // single call to |read_cb_| for more data.
52 int ChunkSize() const;
54 // See SincResampler::BufferedFrames.
55 double BufferedFrames() const;
57 // See SincResampler::PrimeWithSilence.
58 void PrimeWithSilence();
60 private:
61 // SincResampler::ReadCB implementation. ProvideInput() will be called for
62 // each channel (in channel order) as SincResampler needs more data.
63 void ProvideInput(int channel, int frames, float* destination);
65 // Source of data for resampling.
66 ReadCB read_cb_;
68 // Each channel has its own high quality resampler.
69 ScopedVector<SincResampler> resamplers_;
71 // Buffers for audio data going into SincResampler from ReadCB.
72 scoped_ptr<AudioBus> resampler_audio_bus_;
74 // To avoid a memcpy() on the first channel we create a wrapped AudioBus where
75 // the first channel points to the |destination| provided to ProvideInput().
76 scoped_ptr<AudioBus> wrapped_resampler_audio_bus_;
78 // The number of output frames that have successfully been processed during
79 // the current Resample() call.
80 int output_frames_ready_;
82 DISALLOW_COPY_AND_ASSIGN(MultiChannelResampler);
85 } // namespace media
87 #endif // MEDIA_BASE_MULTI_CHANNEL_RESAMPLER_H_