blob: 8b6a62f25638f42d9a311cc92880fff488888fb2 [file] [log] [blame]
Marco Nelissen1a7b4152019-10-23 09:21:55 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_MEDIA_VISUALIZER_H
18#define ANDROID_MEDIA_VISUALIZER_H
19
20#include <media/AudioEffect.h>
21#include <system/audio_effects/effect_visualizer.h>
22#include <utils/Thread.h>
23
24/**
25 * The Visualizer class enables application to retrieve part of the currently playing audio for
26 * visualization purpose. It is not an audio recording interface and only returns partial and low
27 * quality audio content. However, to protect privacy of certain audio data (e.g voice mail) the use
28 * of the visualizer requires the permission android.permission.RECORD_AUDIO.
29 * The audio session ID passed to the constructor indicates which audio content should be
30 * visualized:
31 * - If the session is 0, the audio output mix is visualized
32 * - If the session is not 0, the audio from a particular MediaPlayer or AudioTrack
33 * using this audio session is visualized
34 * Two types of representation of audio content can be captured:
35 * - Waveform data: consecutive 8-bit (unsigned) mono samples by using the getWaveForm() method
36 * - Frequency data: 8-bit magnitude FFT by using the getFft() method
37 *
38 * The length of the capture can be retrieved or specified by calling respectively
39 * getCaptureSize() and setCaptureSize() methods. Note that the size of the FFT
40 * is half of the specified capture size but both sides of the spectrum are returned yielding in a
41 * number of bytes equal to the capture size. The capture size must be a power of 2 in the range
42 * returned by getMinCaptureSize() and getMaxCaptureSize().
43 * In addition to the polling capture mode, a callback mode is also available by installing a
44 * callback function by use of the setCaptureCallBack() method. The rate at which the callback
45 * is called as well as the type of data returned is specified.
46 * Before capturing data, the Visualizer must be enabled by calling the setEnabled() method.
47 * When data capture is not needed any more, the Visualizer should be disabled.
48 */
49
50
51namespace android {
52
53// ----------------------------------------------------------------------------
54
55class Visualizer: public AudioEffect {
56public:
57
58 enum callback_flags {
59 CAPTURE_WAVEFORM = 0x00000001, // capture callback returns a PCM wave form
60 CAPTURE_FFT = 0x00000002, // apture callback returns a frequency representation
61 CAPTURE_CALL_JAVA = 0x00000004 // the callback thread can call java
62 };
63
64
65 /* Constructor.
66 * See AudioEffect constructor for details on parameters.
67 */
Mikhail Naganov12223f02020-07-31 17:42:20 -070068 explicit Visualizer(const String16& opPackageName);
Marco Nelissen1a7b4152019-10-23 09:21:55 -070069
70 ~Visualizer();
71
Mikhail Naganov12223f02020-07-31 17:42:20 -070072 /**
73 * Initialize an uninitialized Visualizer.
74 * See AudioEffect 'set' function for details on parameters.
75 */
76 status_t set(int32_t priority = 0,
77 effect_callback_t cbf = NULL,
78 void* user = NULL,
79 audio_session_t sessionId = AUDIO_SESSION_OUTPUT_MIX,
80 audio_io_handle_t io = AUDIO_IO_HANDLE_NONE,
81 const AudioDeviceTypeAddr& device = {},
82 bool probe = false);
83
Greg Kaiserd4dd8b82019-10-25 12:55:02 -070084 // Declared 'final' because we call this in ~Visualizer().
85 status_t setEnabled(bool enabled) final;
Marco Nelissen1a7b4152019-10-23 09:21:55 -070086
87 // maximum capture size in samples
88 static uint32_t getMaxCaptureSize() { return VISUALIZER_CAPTURE_SIZE_MAX; }
89 // minimum capture size in samples
90 static uint32_t getMinCaptureSize() { return VISUALIZER_CAPTURE_SIZE_MIN; }
91 // maximum capture rate in millihertz
92 static uint32_t getMaxCaptureRate() { return CAPTURE_RATE_MAX; }
93
94 // callback used to return periodic PCM or FFT captures to the application. Either one or both
95 // types of data are returned (PCM and FFT) according to flags indicated when installing the
96 // callback. When a type of data is not present, the corresponding size (waveformSize or
97 // fftSize) is 0.
98 typedef void (*capture_cbk_t)(void* user,
99 uint32_t waveformSize,
100 uint8_t *waveform,
101 uint32_t fftSize,
102 uint8_t *fft,
103 uint32_t samplingrate);
104
105 // install a callback to receive periodic captures. The capture rate is specified in milliHertz
106 // and the capture format is according to flags (see callback_flags).
107 status_t setCaptureCallBack(capture_cbk_t cbk, void* user, uint32_t flags, uint32_t rate);
108
109 // set the capture size capture size must be a power of two in the range
110 // [VISUALIZER_CAPTURE_SIZE_MAX. VISUALIZER_CAPTURE_SIZE_MIN]
111 // must be called when the visualizer is not enabled
112 status_t setCaptureSize(uint32_t size);
113 uint32_t getCaptureSize() { return mCaptureSize; }
114
115 // returns the capture rate indicated when installing the callback
116 uint32_t getCaptureRate() { return mCaptureRate; }
117
118 // returns the sampling rate of the audio being captured
119 uint32_t getSamplingRate() { return mSampleRate; }
120
121 // set the way volume affects the captured data
122 // mode must one of VISUALIZER_SCALING_MODE_NORMALIZED,
123 // VISUALIZER_SCALING_MODE_AS_PLAYED
124 status_t setScalingMode(uint32_t mode);
125 uint32_t getScalingMode() { return mScalingMode; }
126
127 // set which measurements are done on the audio buffers processed by the effect.
128 // valid measurements (mask): MEASUREMENT_MODE_PEAK_RMS
129 status_t setMeasurementMode(uint32_t mode);
130 uint32_t getMeasurementMode() { return mMeasurementMode; }
131
132 // return a set of int32_t measurements
133 status_t getIntMeasurements(uint32_t type, uint32_t number, int32_t *measurements);
134
135 // return a capture in PCM 8 bit unsigned format. The size of the capture is equal to
136 // getCaptureSize()
137 status_t getWaveForm(uint8_t *waveform);
138
139 // return a capture in FFT 8 bit signed format. The size of the capture is equal to
140 // getCaptureSize() but the length of the FFT is half of the size (both parts of the spectrum
141 // are returned
142 status_t getFft(uint8_t *fft);
143 void release();
144
145protected:
146 // from IEffectClient
147 virtual void controlStatusChanged(bool controlGranted);
148
149private:
150
151 static const uint32_t CAPTURE_RATE_MAX = 20000;
152 static const uint32_t CAPTURE_RATE_DEF = 10000;
153 static const uint32_t CAPTURE_SIZE_DEF = VISUALIZER_CAPTURE_SIZE_MAX;
154
155 /* internal class to handle the callback */
156 class CaptureThread : public Thread
157 {
158 public:
159 CaptureThread(Visualizer* visualizer, uint32_t captureRate, bool bCanCallJava = false);
160
161 private:
162 friend class Visualizer;
163 virtual bool threadLoop();
164 wp<Visualizer> mReceiver;
165 Mutex mLock;
166 uint32_t mSleepTimeUs;
167 };
168
169 status_t doFft(uint8_t *fft, uint8_t *waveform);
170 void periodicCapture();
171 uint32_t initCaptureSize();
172
173 Mutex mCaptureLock;
Mikhail Naganov12223f02020-07-31 17:42:20 -0700174 uint32_t mCaptureRate = CAPTURE_RATE_DEF;
175 uint32_t mCaptureSize = CAPTURE_SIZE_DEF;
176 uint32_t mSampleRate = 44100000;
177 uint32_t mScalingMode = VISUALIZER_SCALING_MODE_NORMALIZED;
178 uint32_t mMeasurementMode = MEASUREMENT_MODE_NONE;
179 capture_cbk_t mCaptureCallBack = nullptr;
180 void *mCaptureCbkUser = nullptr;
Marco Nelissen1a7b4152019-10-23 09:21:55 -0700181 sp<CaptureThread> mCaptureThread;
Mikhail Naganov12223f02020-07-31 17:42:20 -0700182 uint32_t mCaptureFlags = 0;
Marco Nelissen1a7b4152019-10-23 09:21:55 -0700183};
184
185
186}; // namespace android
187
188#endif // ANDROID_MEDIA_VISUALIZER_H