1 /*
2  * Copyright (C) 2017 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 #include <chre.h>
18 #include <cinttypes>
19 #include <cmath>
20 
21 #include "chre/util/macros.h"
22 #include "chre/util/nanoapp/audio.h"
23 #include "chre/util/nanoapp/log.h"
24 #include "chre/util/time.h"
25 #include "kiss_fftr.h"
26 
27 #define LOG_TAG "[AudioWorld]"
28 
29 #ifdef CHRE_NANOAPP_INTERNAL
30 namespace chre {
31 namespace {
32 #endif  // CHRE_NANOAPP_INTERNAL
33 
34 using chre::Milliseconds;
35 using chre::Nanoseconds;
36 
37 //! The number of frequencies to generate an FFT over.
38 constexpr size_t kNumFrequencies = 128;
39 
40 //! True if audio has successfully been requested.
41 bool gAudioRequested = false;
42 
43 //! The requested audio handle.
44 uint32_t gAudioHandle;
45 
46 //! State for Kiss FFT and logging.
47 uint8_t gKissFftBuffer[4096];
48 kiss_fftr_cfg gKissFftConfig;
49 kiss_fft_cpx gKissFftOutput[(kNumFrequencies / 2) + 1];
50 Milliseconds gFirstAudioEventTimestamp = Milliseconds(0);
51 
52 /**
53  * Returns a graphical representation of a uint16_t value.
54  *
55  * @param value the value to visualize.
56  * @return a character that visually represents the value.
57  */
getFftCharForValue(uint16_t value)58 char getFftCharForValue(uint16_t value) {
59   constexpr uint16_t kFftLowLimit = 128;
60   constexpr uint16_t kFftMedLimit = 256;
61   constexpr uint16_t kFftHighLimit = 512;  // Texas Hold'em ༼⁰o⁰;༽
62   constexpr uint16_t kFftVeryHighLimit = 1024;
63 
64   if (value < kFftLowLimit) {
65     return ' ';
66   } else if (value >= kFftLowLimit && value < kFftMedLimit) {
67     return '_';
68   } else if (value >= kFftMedLimit && value < kFftHighLimit) {
69     return '.';
70   } else if (value >= kFftHighLimit && value < kFftVeryHighLimit) {
71     return 'x';
72   } else {
73     return 'X';
74   }
75 }
76 
77 /**
78  * Initializes Kiss FFT.
79  */
initKissFft()80 void initKissFft() {
81   size_t kissFftBufferSize = sizeof(gKissFftBuffer);
82   gKissFftConfig = kiss_fftr_alloc(kNumFrequencies, false, gKissFftBuffer,
83                                    &kissFftBufferSize);
84   if (gKissFftConfig == NULL) {
85     LOGE("Failed to init Kiss FFT, needs minimum %zu buffer size",
86          kissFftBufferSize);
87   } else {
88     LOGI("Initialized Kiss FFT, using %zu/%zu of the buffer", kissFftBufferSize,
89          sizeof(gKissFftBuffer));
90   }
91 }
92 
93 /**
94  * Logs an audio data event with an FFT visualization of the received audio
95  * data.
96  *
97  * @param event the audio data event to log.
98  */
handleAudioDataEvent(const struct chreAudioDataEvent * event)99 void handleAudioDataEvent(const struct chreAudioDataEvent *event) {
100   kiss_fftr(gKissFftConfig, event->samplesS16, gKissFftOutput);
101 
102   char fftStr[ARRAY_SIZE(gKissFftOutput) + 1];
103   fftStr[ARRAY_SIZE(gKissFftOutput)] = '\0';
104 
105   for (size_t i = 0; i < ARRAY_SIZE(gKissFftOutput); i++) {
106     float value =
107         sqrtf(powf(gKissFftOutput[i].r, 2) + powf(gKissFftOutput[i].i, 2));
108     fftStr[i] = getFftCharForValue(static_cast<uint16_t>(value));
109   }
110 
111   Milliseconds timestamp = Milliseconds(Nanoseconds(event->timestamp));
112   if (gFirstAudioEventTimestamp == Milliseconds(0)) {
113     gFirstAudioEventTimestamp = timestamp;
114   }
115 
116   Milliseconds adjustedTimestamp = timestamp - gFirstAudioEventTimestamp;
117   LOGD("Audio data - FFT [%s] at %" PRIu64 "ms with %" PRIu32 " samples",
118        fftStr, adjustedTimestamp.getMilliseconds(), event->sampleCount);
119 }
120 
handleAudioSamplingChangeEvent(const struct chreAudioSourceStatusEvent * event)121 void handleAudioSamplingChangeEvent(
122     const struct chreAudioSourceStatusEvent *event) {
123   LOGD("Audio sampling status event for handle %" PRIu32 ", suspended: %d",
124        event->handle, event->status.suspended);
125 }
126 
handleAudioSettingChangedNotification(const struct chreUserSettingChangedEvent * event)127 void handleAudioSettingChangedNotification(
128     const struct chreUserSettingChangedEvent *event) {
129   // The following checks on event and setting are primarily meant for
130   // debugging and/or bring-up. Production nanoapps should not need to worry
131   // about these scenarios since CHRE guarantees that out-of-memory conditions
132   // are caught during event allocation before they're posted, and the setting
133   // is guaranteed to be a member of enum chreUserSettingState.
134   if (event != nullptr) {
135     if (event->setting != CHRE_USER_SETTING_MICROPHONE) {
136       LOGE("Unexpected setting ID: %u", event->setting);
137     } else {
138       LOGI("Microphone settings notification: status change to %d",
139            static_cast<int8_t>(event->settingState));
140     }
141   } else {
142     LOGE("Null event data for settings changed event");
143   }
144 }
145 
nanoappStart()146 bool nanoappStart() {
147   LOGI("Started");
148 
149   struct chreAudioSource audioSource;
150   for (uint32_t i = 0; chreAudioGetSource(i, &audioSource); i++) {
151     LOGI("Found audio source '%s' with %" PRIu32 "Hz %s data", audioSource.name,
152          audioSource.sampleRate,
153          chre::getChreAudioFormatString(audioSource.format));
154     LOGI("  buffer duration: [%" PRIu64 "ns, %" PRIu64 "ns]",
155          audioSource.minBufferDuration, audioSource.maxBufferDuration);
156 
157     if (i == 0) {
158       // Only request audio data from the first source, but continue discovery.
159       if (chreAudioConfigureSource(i, true, audioSource.minBufferDuration,
160                                    audioSource.minBufferDuration)) {
161         gAudioRequested = true;
162         gAudioHandle = i;
163         LOGI("Requested audio from handle %" PRIu32 " successfully", i);
164       } else {
165         LOGE("Failed to request audio from handle %" PRIu32, i);
166       }
167     }
168   }
169 
170   initKissFft();
171 
172   int8_t settingState = chreUserSettingGetState(CHRE_USER_SETTING_MICROPHONE);
173   LOGD("Microphone setting status: %d", settingState);
174 
175   chreUserSettingConfigureEvents(CHRE_USER_SETTING_MICROPHONE,
176                                  true /* enable */);
177 
178   return true;
179 }
180 
nanoappHandleEvent(uint32_t senderInstanceId,uint16_t eventType,const void * eventData)181 void nanoappHandleEvent(uint32_t senderInstanceId, uint16_t eventType,
182                         const void *eventData) {
183   switch (eventType) {
184     case CHRE_EVENT_AUDIO_DATA:
185       handleAudioDataEvent(
186           static_cast<const struct chreAudioDataEvent *>(eventData));
187       break;
188     case CHRE_EVENT_AUDIO_SAMPLING_CHANGE:
189       handleAudioSamplingChangeEvent(
190           static_cast<const struct chreAudioSourceStatusEvent *>(eventData));
191       break;
192 
193     case CHRE_EVENT_SETTING_CHANGED_MICROPHONE:
194       handleAudioSettingChangedNotification(
195           static_cast<const struct chreUserSettingChangedEvent *>(eventData));
196       break;
197 
198     default:
199       LOGW("Unknown event received");
200       break;
201   }
202 }
203 
nanoappEnd()204 void nanoappEnd() {
205   if (gAudioRequested) {
206     chreAudioConfigureSource(gAudioHandle, false /* enable */,
207                              0 /* bufferDuration */, 0 /* deliveryInterval */);
208   }
209 
210   chreUserSettingConfigureEvents(CHRE_USER_SETTING_MICROPHONE,
211                                  false /* enable */);
212 
213   LOGI("Stopped");
214 }
215 
216 #ifdef CHRE_NANOAPP_INTERNAL
217 }  // anonymous namespace
218 }  // namespace chre
219 
220 #include "chre/platform/static_nanoapp_init.h"
221 #include "chre/util/nanoapp/app_id.h"
222 #include "chre/util/system/napp_permissions.h"
223 
224 CHRE_STATIC_NANOAPP_INIT(AudioWorld, chre::kAudioWorldAppId, 0,
225                          chre::NanoappPermissions::CHRE_PERMS_AUDIO);
226 #endif  // CHRE_NANOAPP_INTERNAL
227