1 /*
2  * Copyright (C) 2016 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 CHRE_PLATFORM_LINUX_AUDIO_SOURCE_H_
18 #define CHRE_PLATFORM_LINUX_AUDIO_SOURCE_H_
19 
20 #include <sndfile.h>
21 #include <string>
22 
23 #include "chre/platform/system_timer.h"
24 #include "chre/util/non_copyable.h"
25 #include "chre/util/time.h"
26 #include "chre_api/chre/audio.h"
27 
28 namespace chre {
29 
30 /**
31  * Maintains the state of one audio source for the simulation environment.
32  */
33 class AudioSource : public NonCopyable {
34  public:
35   /**
36    * Constructs an audio source.
37    *
38    * @param audioFilename the filename for the file to open and playback.
39    * @param minBufferSize the minimum buffer size, in seconds, to provide to a
40    *        nanoapp.
41    * @param maxBufferSize the maximum buffer size, in seconds, to provide to a
42    *        nanoapp.
43    */
44   AudioSource(const std::string &audioFilename, double minBufferDuration,
45               double maxBufferDuration);
46 
47   /**
48    * Releases the audio buffer created during initialization.
49    */
50   ~AudioSource();
51 
52   //! The audio file to open and playback to the nanoapp.
53   const std::string audioFilename;
54 
55   //! The minimum buffer duration for this audio source.
56   const Nanoseconds minBufferDuration;
57 
58   //! The maximum buffer duration for this audio source.
59   const Nanoseconds maxBufferDuration;
60 
61   //! The libsndfile for this audio file.
62   SNDFILE *audioFile = nullptr;
63 
64   //! The libsndfile audio info for this source.
65   SF_INFO audioInfo;
66 
67   //! The number of samples for the current request to this source.
68   uint32_t numSamples;
69 
70   //! The amount of time to wait before delivering this event.
71   Nanoseconds eventDelay;
72 
73   //! The timer used to delay sending audio data to CHRE.
74   SystemTimer timer;
75 
76   //! The audio data event to publish to CHRE.
77   chreAudioDataEvent dataEvent;
78 };
79 
80 }  // namespace chre
81 
82 #endif  // CHRE_PLATFORM_LINUX_AUDIO_SOURCE_H_
83