Home | History | Annotate | Download | only in audio_processing
      1 /*
      2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #ifndef WEBRTC_MODULES_AUDIO_PROCESSING_MAIN_SOURCE_AUDIO_PROCESSING_IMPL_H_
     12 #define WEBRTC_MODULES_AUDIO_PROCESSING_MAIN_SOURCE_AUDIO_PROCESSING_IMPL_H_
     13 
     14 #include "webrtc/modules/audio_processing/include/audio_processing.h"
     15 
     16 #include <list>
     17 #include <string>
     18 
     19 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
     20 
     21 namespace webrtc {
     22 
     23 class AudioBuffer;
     24 class CriticalSectionWrapper;
     25 class EchoCancellationImpl;
     26 class EchoControlMobileImpl;
     27 class FileWrapper;
     28 class GainControlImpl;
     29 class HighPassFilterImpl;
     30 class LevelEstimatorImpl;
     31 class NoiseSuppressionImpl;
     32 class ProcessingComponent;
     33 class VoiceDetectionImpl;
     34 
     35 #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
     36 namespace audioproc {
     37 
     38 class Event;
     39 
     40 }  // namespace audioproc
     41 #endif
     42 
     43 class AudioRate {
     44  public:
     45   explicit AudioRate(int sample_rate_hz)
     46       : rate_(sample_rate_hz),
     47         samples_per_channel_(AudioProcessing::kChunkSizeMs * rate_ / 1000) {}
     48   virtual ~AudioRate() {}
     49 
     50   void set(int rate) {
     51     rate_ = rate;
     52     samples_per_channel_ = AudioProcessing::kChunkSizeMs * rate_ / 1000;
     53   }
     54 
     55   int rate() const { return rate_; }
     56   int samples_per_channel() const { return samples_per_channel_; }
     57 
     58  private:
     59   int rate_;
     60   int samples_per_channel_;
     61 };
     62 
     63 class AudioFormat : public AudioRate {
     64  public:
     65   AudioFormat(int sample_rate_hz, int num_channels)
     66       : AudioRate(sample_rate_hz),
     67         num_channels_(num_channels) {}
     68   virtual ~AudioFormat() {}
     69 
     70   void set(int rate, int num_channels) {
     71     AudioRate::set(rate);
     72     num_channels_ = num_channels;
     73   }
     74 
     75   int num_channels() const { return num_channels_; }
     76 
     77  private:
     78   int num_channels_;
     79 };
     80 
     81 class AudioProcessingImpl : public AudioProcessing {
     82  public:
     83   explicit AudioProcessingImpl(const Config& config);
     84   virtual ~AudioProcessingImpl();
     85 
     86   // AudioProcessing methods.
     87   virtual int Initialize() OVERRIDE;
     88   virtual int Initialize(int input_sample_rate_hz,
     89                          int output_sample_rate_hz,
     90                          int reverse_sample_rate_hz,
     91                          ChannelLayout input_layout,
     92                          ChannelLayout output_layout,
     93                          ChannelLayout reverse_layout) OVERRIDE;
     94   virtual void SetExtraOptions(const Config& config) OVERRIDE;
     95   virtual int set_sample_rate_hz(int rate) OVERRIDE;
     96   virtual int input_sample_rate_hz() const OVERRIDE;
     97   virtual int sample_rate_hz() const OVERRIDE;
     98   virtual int proc_sample_rate_hz() const OVERRIDE;
     99   virtual int proc_split_sample_rate_hz() const OVERRIDE;
    100   virtual int num_input_channels() const OVERRIDE;
    101   virtual int num_output_channels() const OVERRIDE;
    102   virtual int num_reverse_channels() const OVERRIDE;
    103   virtual void set_output_will_be_muted(bool muted) OVERRIDE;
    104   virtual bool output_will_be_muted() const OVERRIDE;
    105   virtual int ProcessStream(AudioFrame* frame) OVERRIDE;
    106   virtual int ProcessStream(const float* const* src,
    107                             int samples_per_channel,
    108                             int input_sample_rate_hz,
    109                             ChannelLayout input_layout,
    110                             int output_sample_rate_hz,
    111                             ChannelLayout output_layout,
    112                             float* const* dest) OVERRIDE;
    113   virtual int AnalyzeReverseStream(AudioFrame* frame) OVERRIDE;
    114   virtual int AnalyzeReverseStream(const float* const* data,
    115                                    int samples_per_channel,
    116                                    int sample_rate_hz,
    117                                    ChannelLayout layout) OVERRIDE;
    118   virtual int set_stream_delay_ms(int delay) OVERRIDE;
    119   virtual int stream_delay_ms() const OVERRIDE;
    120   virtual bool was_stream_delay_set() const OVERRIDE;
    121   virtual void set_delay_offset_ms(int offset) OVERRIDE;
    122   virtual int delay_offset_ms() const OVERRIDE;
    123   virtual void set_stream_key_pressed(bool key_pressed) OVERRIDE;
    124   virtual bool stream_key_pressed() const OVERRIDE;
    125   virtual int StartDebugRecording(
    126       const char filename[kMaxFilenameSize]) OVERRIDE;
    127   virtual int StartDebugRecording(FILE* handle) OVERRIDE;
    128   virtual int StartDebugRecordingForPlatformFile(
    129       rtc::PlatformFile handle) OVERRIDE;
    130   virtual int StopDebugRecording() OVERRIDE;
    131   virtual EchoCancellation* echo_cancellation() const OVERRIDE;
    132   virtual EchoControlMobile* echo_control_mobile() const OVERRIDE;
    133   virtual GainControl* gain_control() const OVERRIDE;
    134   virtual HighPassFilter* high_pass_filter() const OVERRIDE;
    135   virtual LevelEstimator* level_estimator() const OVERRIDE;
    136   virtual NoiseSuppression* noise_suppression() const OVERRIDE;
    137   virtual VoiceDetection* voice_detection() const OVERRIDE;
    138 
    139  protected:
    140   // Overridden in a mock.
    141   virtual int InitializeLocked();
    142 
    143  private:
    144   int InitializeLocked(int input_sample_rate_hz,
    145                        int output_sample_rate_hz,
    146                        int reverse_sample_rate_hz,
    147                        int num_input_channels,
    148                        int num_output_channels,
    149                        int num_reverse_channels);
    150   int MaybeInitializeLocked(int input_sample_rate_hz,
    151                             int output_sample_rate_hz,
    152                             int reverse_sample_rate_hz,
    153                             int num_input_channels,
    154                             int num_output_channels,
    155                             int num_reverse_channels);
    156   int ProcessStreamLocked();
    157   int AnalyzeReverseStreamLocked();
    158 
    159   bool is_data_processed() const;
    160   bool output_copy_needed(bool is_data_processed) const;
    161   bool synthesis_needed(bool is_data_processed) const;
    162   bool analysis_needed(bool is_data_processed) const;
    163 
    164   EchoCancellationImpl* echo_cancellation_;
    165   EchoControlMobileImpl* echo_control_mobile_;
    166   GainControlImpl* gain_control_;
    167   HighPassFilterImpl* high_pass_filter_;
    168   LevelEstimatorImpl* level_estimator_;
    169   NoiseSuppressionImpl* noise_suppression_;
    170   VoiceDetectionImpl* voice_detection_;
    171 
    172   std::list<ProcessingComponent*> component_list_;
    173   CriticalSectionWrapper* crit_;
    174   scoped_ptr<AudioBuffer> render_audio_;
    175   scoped_ptr<AudioBuffer> capture_audio_;
    176 #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
    177   // TODO(andrew): make this more graceful. Ideally we would split this stuff
    178   // out into a separate class with an "enabled" and "disabled" implementation.
    179   int WriteMessageToDebugFile();
    180   int WriteInitMessage();
    181   scoped_ptr<FileWrapper> debug_file_;
    182   scoped_ptr<audioproc::Event> event_msg_;  // Protobuf message.
    183   std::string event_str_;  // Memory for protobuf serialization.
    184 #endif
    185 
    186   AudioFormat fwd_in_format_;
    187   AudioFormat fwd_proc_format_;
    188   AudioRate fwd_out_format_;
    189   AudioFormat rev_in_format_;
    190   AudioFormat rev_proc_format_;
    191   int split_rate_;
    192 
    193   int stream_delay_ms_;
    194   int delay_offset_ms_;
    195   bool was_stream_delay_set_;
    196 
    197   bool output_will_be_muted_;
    198 
    199   bool key_pressed_;
    200 };
    201 
    202 }  // namespace webrtc
    203 
    204 #endif  // WEBRTC_MODULES_AUDIO_PROCESSING_MAIN_SOURCE_AUDIO_PROCESSING_IMPL_H_
    205