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 #ifndef MEDIA_AUDIO_SIMPLE_SOURCES_H_ | 5 #ifndef MEDIA_AUDIO_SIMPLE_SOURCES_H_ |
6 #define MEDIA_AUDIO_SIMPLE_SOURCES_H_ | 6 #define MEDIA_AUDIO_SIMPLE_SOURCES_H_ |
7 | 7 |
8 #include <list> | 8 #include "base/synchronization/lock.h" |
9 | |
10 #include "media/audio/audio_io.h" | 9 #include "media/audio/audio_io.h" |
11 #include "media/base/seekable_buffer.h" | 10 #include "media/base/seekable_buffer.h" |
12 | 11 |
13 namespace media { | 12 namespace media { |
14 | 13 |
15 // An audio source that produces a pure sinusoidal tone. | 14 // An audio source that produces a pure sinusoidal tone. |
16 class MEDIA_EXPORT SineWaveAudioSource | 15 class MEDIA_EXPORT SineWaveAudioSource |
17 : public AudioOutputStream::AudioSourceCallback { | 16 : public AudioOutputStream::AudioSourceCallback { |
18 public: | 17 public: |
19 enum Format { | |
20 FORMAT_8BIT_LINEAR_PCM, | |
21 FORMAT_16BIT_LINEAR_PCM, | |
22 }; | |
23 // |channels| is the number of audio channels, |freq| is the frequency in | 18 // |channels| is the number of audio channels, |freq| is the frequency in |
24 // hertz and it has to be less than half of the sampling frequency | 19 // hertz and it has to be less than half of the sampling frequency |
25 // |sample_freq| or else you will get aliasing. | 20 // |sample_freq| or else you will get aliasing. |
26 SineWaveAudioSource(Format format, int channels, | 21 SineWaveAudioSource(int channels, double freq, double sample_freq); |
27 double freq, double sample_freq); | |
28 virtual ~SineWaveAudioSource() {} | 22 virtual ~SineWaveAudioSource() {} |
29 | 23 |
24 // Return up to |cap| samples of data via OnMoreData(). Use Reset() to | |
25 // allow more data to be served. | |
26 void CapSamples(int cap); | |
27 void Reset(); | |
28 | |
30 // Implementation of AudioSourceCallback. | 29 // Implementation of AudioSourceCallback. |
31 virtual uint32 OnMoreData( | 30 virtual int OnMoreData(AudioBus* audio_bus, |
32 uint8* dest, uint32 max_size, AudioBuffersState audio_buffers) OVERRIDE; | 31 AudioBuffersState audio_buffers) OVERRIDE; |
33 virtual void OnError(AudioOutputStream* stream, int code) OVERRIDE; | 32 virtual void OnError(AudioOutputStream* stream, int code) OVERRIDE; |
34 | 33 |
35 protected: | 34 protected: |
36 Format format_; | |
37 int channels_; | 35 int channels_; |
38 double freq_; | 36 double f_; |
39 double sample_freq_; | |
40 int time_state_; | 37 int time_state_; |
41 }; | 38 int cap_; |
42 | 39 base::Lock time_lock_; |
43 // Defines an interface for pushing audio output. In contrast, the interfaces | |
44 // defined by AudioSourceCallback are pull model only. | |
45 class MEDIA_EXPORT PushAudioOutput { | |
DaleCurtis
2012/08/22 23:09:21
Replaced with CapSamples/Reset API on SineWaveAudi
| |
46 public: | |
47 virtual ~PushAudioOutput() {} | |
48 | |
49 // Write audio data to the audio device. It will be played eventually. | |
50 // Returns false on failure. | |
51 virtual bool Write(const void* data, uint32 len) = 0; | |
52 | |
53 // Returns the number of bytes that have been buffered but not yet given | |
54 // to the audio device. | |
55 virtual uint32 UnProcessedBytes() = 0; | |
56 }; | |
57 | |
58 // A fairly basic class to connect a push model provider PushAudioOutput to | |
59 // a pull model provider AudioSourceCallback. Fundamentally it manages a series | |
60 // of audio buffers and is unaware of the actual audio format. | |
61 // Note that the PushSource is not thread safe and user need to provide locking. | |
62 class MEDIA_EXPORT PushSource | |
63 : public AudioOutputStream::AudioSourceCallback, | |
64 public PushAudioOutput { | |
65 public: | |
66 PushSource(); | |
67 virtual ~PushSource(); | |
68 | |
69 // Write one buffer. | |
70 virtual bool Write(const void* data, uint32 len) OVERRIDE; | |
71 | |
72 // Return the total number of bytes not given to the audio device yet. | |
73 virtual uint32 UnProcessedBytes() OVERRIDE; | |
74 | |
75 // Implementation of AudioSourceCallback. | |
76 virtual uint32 OnMoreData(uint8* dest, | |
77 uint32 max_size, | |
78 AudioBuffersState buffers_state) OVERRIDE; | |
79 virtual void OnError(AudioOutputStream* stream, int code) OVERRIDE; | |
80 | |
81 private: | |
82 // Free acquired resources. | |
83 void CleanUp(); | |
84 | |
85 media::SeekableBuffer buffer_; | |
86 }; | 40 }; |
87 | 41 |
88 } // namespace media | 42 } // namespace media |
89 | 43 |
90 #endif // MEDIA_AUDIO_SIMPLE_SOURCES_H_ | 44 #endif // MEDIA_AUDIO_SIMPLE_SOURCES_H_ |
OLD | NEW |