audio_converter.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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. 4 5#ifndef MEDIA_BASE_AUDIO_CONVERTER_H_ 6#define MEDIA_BASE_AUDIO_CONVERTER_H_ 7 8#include <list> 9 10#include "base/callback.h" 11#include "base/time.h" 12#include "media/audio/audio_parameters.h" 13#include "media/base/media_export.h" 14 15namespace media { 16 17class AudioBus; 18class AudioPullFifo; 19class ChannelMixer; 20class MultiChannelResampler; 21 22// AudioConverter is a complete mixing, resampling, buffering, and channel 23// mixing solution for converting data from one set of AudioParameters to 24// another. For efficiency pieces are only invoked when necessary; e.g. the 25// resampler is only used if the input and output sample rates differ. Mixing 26// and channel down mixing are done prior to resampling to maximize efficiency. 27class MEDIA_EXPORT AudioConverter { 28 public: 29 class MEDIA_EXPORT InputCallback { 30 public: 31 // Method for providing more data into the converter. Expects |audio_bus| 32 // to be completely filled with data upon return; zero padded if not enough 33 // frames are available to satisfy the request. The return value is the 34 // volume level of the provided audio data. If a volume level of zero is 35 // returned no further processing will be done on the provided data, else 36 // the volume level will be used to scale the provided audio data. 37 virtual double ProvideInput(AudioBus* audio_bus, 38 base::TimeDelta buffer_delay) = 0; 39 40 protected: 41 virtual ~InputCallback() {} 42 }; 43 44 // Construct an AudioConverter for converting between the given input and 45 // output parameters. Specifying |disable_fifo| means all InputCallbacks are 46 // capable of handling arbitrary buffer size requests; i.e. one call might ask 47 // for 10 frames of data (indicated by the size of AudioBus provided) and the 48 // next might ask for 20. In synthetic testing, disabling the FIFO yields a 49 // ~20% speed up for common cases. 50 AudioConverter(const AudioParameters& input_params, 51 const AudioParameters& output_params, 52 bool disable_fifo); 53 ~AudioConverter(); 54 55 // Converts audio from all inputs into the |dest|. |dest| must be sized for 56 // data matching the output AudioParameters provided during construction. 57 void Convert(AudioBus* dest); 58 59 // Add or remove an input from the converter. 60 void AddInput(InputCallback* input); 61 void RemoveInput(InputCallback* input); 62 63 // Flush all buffered data. Automatically called when all inputs are removed. 64 void Reset(); 65 66 private: 67 // Called by MultiChannelResampler when more data is necessary. 68 void ProvideInput(int resampler_frame_delay, AudioBus* audio_bus); 69 70 // Called by AudioPullFifo when more data is necessary. 71 void SourceCallback(int fifo_frame_delay, AudioBus* audio_bus); 72 73 // Set of inputs for Convert(). 74 typedef std::list<InputCallback*> InputCallbackSet; 75 InputCallbackSet transform_inputs_; 76 77 // Used to buffer data between the client and the output device in cases where 78 // the client buffer size is not the same as the output device buffer size. 79 scoped_ptr<AudioPullFifo> audio_fifo_; 80 81 // Handles resampling. 82 scoped_ptr<MultiChannelResampler> resampler_; 83 84 // Handles channel transforms. |unmixed_audio_| is a temporary destination 85 // for audio data before it goes into the channel mixer. 86 scoped_ptr<ChannelMixer> channel_mixer_; 87 scoped_ptr<AudioBus> unmixed_audio_; 88 89 // Temporary AudioBus destination for mixing inputs. 90 scoped_ptr<AudioBus> mixer_input_audio_bus_; 91 92 // Since resampling is expensive, figure out if we should downmix channels 93 // before resampling. 94 bool downmix_early_; 95 96 // Used to calculate buffer delay information for InputCallbacks. 97 base::TimeDelta input_frame_duration_; 98 base::TimeDelta output_frame_duration_; 99 int resampler_frame_delay_; 100 101 const int input_channel_count_; 102 103 DISALLOW_COPY_AND_ASSIGN(AudioConverter); 104}; 105 106} // namespace media 107 108#endif // MEDIA_BASE_AUDIO_CONVERTER_H_ 109