summaryrefslogtreecommitdiffstats
path: root/xbmc/addons/kodi-addon-dev-kit/include/kodi/AudioEngine.h
diff options
context:
space:
mode:
Diffstat (limited to 'xbmc/addons/kodi-addon-dev-kit/include/kodi/AudioEngine.h')
-rw-r--r--xbmc/addons/kodi-addon-dev-kit/include/kodi/AudioEngine.h580
1 files changed, 580 insertions, 0 deletions
diff --git a/xbmc/addons/kodi-addon-dev-kit/include/kodi/AudioEngine.h b/xbmc/addons/kodi-addon-dev-kit/include/kodi/AudioEngine.h
new file mode 100644
index 0000000..380e5e2
--- /dev/null
+++ b/xbmc/addons/kodi-addon-dev-kit/include/kodi/AudioEngine.h
@@ -0,0 +1,580 @@
1#pragma once
2/*
3 * Copyright (C) 2005-2017 Team KODI
4 * http://kodi.tv
5 *
6 * This Program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
10 *
11 * This Program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Kodi; see the file COPYING. If not, see
18 * <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#include "AddonBase.h"
23
24#ifdef BUILD_KODI_ADDON
25#include "AEChannelData.h"
26#else
27#include "cores/AudioEngine/Utils/AEChannelData.h"
28#endif
29
30//==============================================================================
31///
32/// \defgroup cpp_kodi_audioengine Interface - kodi::audioengine
33/// \ingroup cpp
34/// @brief **Audio engine functions**
35///
36///
37/// It has the header \ref AudioEngine.h "#include <kodi/AudioEngine.h>" be included
38/// to enjoy it.
39///
40//------------------------------------------------------------------------------
41
42//==============================================================================
43///
44/// \defgroup cpp_kodi_audioengine_Defs Definitions, structures and enumerators
45/// \ingroup cpp_kodi_audioengine
46/// @brief **Library definition values**
47///
48//------------------------------------------------------------------------------
49
50extern "C"
51{
52
53 //============================================================================
54 /// \ingroup cpp_kodi_audioengine_Defs
55 /// @brief Bit options to pass to CAddonAEStream
56 ///
57 typedef enum AudioEngineStreamOptions
58 {
59 /// force resample even if rates match
60 AUDIO_STREAM_FORCE_RESAMPLE = 1 << 0,
61 /// create the stream paused
62 AUDIO_STREAM_PAUSED = 1 << 1,
63 /// autostart the stream when enough data is buffered
64 AUDIO_STREAM_AUTOSTART = 1 << 2,
65 /// if this option is set the ADSP-System is bypassed and the raw stream
66 /// will be passed through IAESink
67 AUDIO_STREAM_BYPASS_ADSP = 1 << 3
68 } AudioEngineStreamOptions;
69 //----------------------------------------------------------------------------
70
71 //============================================================================
72 /// \defgroup cpp_kodi_audioengine_Defs_AudioEngineFormat struct AudioEngineFormat
73 /// \ingroup cpp_kodi_audioengine_Defs
74 /// @brief The audio format structure that fully defines a stream's audio
75 /// information
76 ///
77 //@{
78 struct AudioEngineFormat
79 {
80 /// The stream's data format (eg, AE_FMT_S16LE)
81 enum AEDataFormat m_dataFormat;
82
83 /// The stream's sample rate (eg, 48000)
84 unsigned int m_sampleRate;
85
86 /// The encoded streams sample rate if a bitstream, otherwise undefined
87 unsigned int m_encodedRate;
88
89 /// The amount of used speaker channels
90 unsigned int m_channelCount;
91
92 /// The stream's channel layout
93 enum AEChannel m_channels[AE_CH_MAX];
94
95 /// The number of frames per period
96 unsigned int m_frames;
97
98 /// The size of one frame in bytes
99 unsigned int m_frameSize;
100
101 AudioEngineFormat()
102 {
103 m_dataFormat = AE_FMT_INVALID;
104 m_sampleRate = 0;
105 m_encodedRate = 0;
106 m_frames = 0;
107 m_frameSize = 0;
108 m_channelCount = 0;
109
110 for (unsigned int ch = 0; ch < AE_CH_MAX; ++ch)
111 {
112 m_channels[ch] = AE_CH_MAX;
113 }
114 }
115
116 /// Function to compare the format structure with another
117 bool compareFormat(const AudioEngineFormat *fmt)
118 {
119 if (!fmt)
120 {
121 return false;
122 }
123
124 if (m_dataFormat != fmt->m_dataFormat ||
125 m_sampleRate != fmt->m_sampleRate ||
126 m_encodedRate != fmt->m_encodedRate ||
127 m_frames != fmt->m_frames ||
128 m_frameSize != fmt->m_frameSize ||
129 m_channelCount != fmt->m_channelCount)
130 {
131 return false;
132 }
133
134 for (unsigned int ch = 0; ch < AE_CH_MAX; ++ch)
135 {
136 if (fmt->m_channels[ch] != m_channels[ch])
137 {
138 return false;
139 }
140 }
141
142 return true;
143 }
144 };
145 //@}
146 //----------------------------------------------------------------------------
147
148 /* A stream handle pointer, which is only used internally by the addon stream handle */
149 typedef void AEStreamHandle;
150
151 /*
152 * Function address structure, not need to visible on dev kit doxygen
153 * documentation
154 */
155 typedef struct AddonToKodiFuncTable_kodi_audioengine
156 {
157 AEStreamHandle* (*MakeStream) (void *kodiBase, AudioEngineFormat format, unsigned int options);
158 void (*FreeStream) (void *kodiBase, AEStreamHandle *stream);
159 bool (*GetCurrentSinkFormat) (void *kodiBase, AudioEngineFormat *SinkFormat);
160
161 // Audio Engine Stream definitions
162 unsigned int (*AEStream_GetSpace) (void *kodiBase, AEStreamHandle *handle);
163 unsigned int (*AEStream_AddData) (void *kodiBase, AEStreamHandle *handle, uint8_t* const *Data, unsigned int Offset, unsigned int Frames);
164 double (*AEStream_GetDelay)(void *kodiBase, AEStreamHandle *handle);
165 bool (*AEStream_IsBuffering)(void *kodiBase, AEStreamHandle *handle);
166 double (*AEStream_GetCacheTime)(void *kodiBase, AEStreamHandle *handle);
167 double (*AEStream_GetCacheTotal)(void *kodiBase, AEStreamHandle *handle);
168 void (*AEStream_Pause)(void *kodiBase, AEStreamHandle *handle);
169 void (*AEStream_Resume)(void *kodiBase, AEStreamHandle *handle);
170 void (*AEStream_Drain)(void *kodiBase, AEStreamHandle *handle, bool Wait);
171 bool (*AEStream_IsDraining)(void *kodiBase, AEStreamHandle *handle);
172 bool (*AEStream_IsDrained)(void *kodiBase, AEStreamHandle *handle);
173 void (*AEStream_Flush)(void *kodiBase, AEStreamHandle *handle);
174 float (*AEStream_GetVolume)(void *kodiBase, AEStreamHandle *handle);
175 void (*AEStream_SetVolume)(void *kodiBase, AEStreamHandle *handle, float Volume);
176 float (*AEStream_GetAmplification)(void *kodiBase, AEStreamHandle *handle);
177 void (*AEStream_SetAmplification)(void *kodiBase, AEStreamHandle *handle, float Amplify);
178 const unsigned int (*AEStream_GetFrameSize)(void *kodiBase, AEStreamHandle *handle);
179 const unsigned int (*AEStream_GetChannelCount)(void *kodiBase, AEStreamHandle *handle);
180 const unsigned int (*AEStream_GetSampleRate)(void *kodiBase, AEStreamHandle *handle);
181 const AEDataFormat (*AEStream_GetDataFormat)(void *kodiBase, AEStreamHandle *handle);
182 double (*AEStream_GetResampleRatio)(void *kodiBase, AEStreamHandle *handle);
183 void (*AEStream_SetResampleRatio)(void *kodiBase, AEStreamHandle *handle, double Ratio);
184 } AddonToKodiFuncTable_kodi_audioengine;
185
186} /* extern "C" */
187
188namespace kodi
189{
190namespace audioengine
191{
192
193 //============================================================================
194 ///
195 /// \defgroup cpp_kodi_audioengine_CAddonAEStream class CAddonAEStream
196 /// \ingroup cpp_kodi_audioengine
197 /// @brief **Audio Engine Stream Class**
198 ///
199 ///
200 /// It has the header \ref AudioEngine.h "#include <kodi/AudioEngine.h>" be
201 /// included to enjoy it.
202 ///
203 //----------------------------------------------------------------------------
204 class CAddonAEStream
205 {
206 public:
207 //==========================================================================
208 /// @ingroup cpp_kodi_audioengine_CAddonAEStream
209 /// @brief Contructs new class to an Kodi IAEStream in the format specified.
210 ///
211 /// @param[in] format The data format the incoming audio will be in
212 /// (e.g. \ref AE_FMT_S16LE)
213 /// @param[in] options [opt] A bit field of stream options (see: enum \ref AudioEngineStreamOptions)
214 ///
215 ///
216 /// ------------------------------------------------------------------------
217 ///
218 /// **Audio engine format information:**
219 /// @code
220 /// /*
221 /// * Audio engine format information
222 /// *
223 /// * Only as example shown here! See always the original structure on related header.
224 /// */
225 /// typedef struct AudioEngineFormat
226 /// {
227 /// enum AEDataFormat m_dataFormat; /* The stream's data format (eg, AE_FMT_S16LE) */
228 /// unsigned int m_sampleRate; /* The stream's sample rate (eg, 48000) */
229 /// unsigned int m_encodedRate; /* The encoded streams sample rate if a bitstream, otherwise undefined */
230 /// unsigned int m_channelCount; /* The amount of used speaker channels */
231 /// enum AEChannel m_channels[AE_CH_MAX]; /* The stream's channel layout */
232 /// unsigned int m_frames; /* The number of frames per period */
233 /// unsigned int m_frameSamples; /* The number of samples in one frame */
234 /// unsigned int m_frameSize; /* The size of one frame in bytes */
235 ///
236 /// /* Function to compare the format structure with another */
237 /// bool compareFormat(const AudioEngineFormat *fmt);
238 /// } AudioEngineFormat;
239 /// @endcode
240 ///
241 /// ------------------------------------------------------------------------
242 ///
243 /// **Bit options to pass to CAELib_Stream (on Kodi by <c>IAE::MakeStream</c>)**
244 ///
245 /// | enum AEStreamOptions | Value: | Description:
246 /// |-------------------------:|:------:|:-----------------------------------
247 /// | AE_STREAM_FORCE_RESAMPLE | 1 << 0 | Force resample even if rates match
248 /// | AE_STREAM_PAUSED | 1 << 1 | Create the stream paused
249 /// | AE_STREAM_AUTOSTART | 1 << 2 | Autostart the stream when enough data is buffered
250 /// | AE_STREAM_BYPASS_ADSP | 1 << 3 | if this option is set the ADSP-System is bypassed and the raw stream will be passed through IAESink.
251 ///
252 ///
253 /// ------------------------------------------------------------------------
254 ///
255 /// **Example:**
256 /// ~~~~~~~~~~~~~{.cpp}
257 ///
258 /// #include <kodi/AudioEngine.h>
259 ///
260 /// using namespace kodi::audioengine;
261 ///
262 /// ...
263 ///
264 /// CAddonAEStream* stream = new CAddonAEStream(AE_FMT_S16LE, AE_STREAM_AUTOSTART | AE_STREAM_BYPASS_ADSP);
265 ///
266 /// ~~~~~~~~~~~~~
267 ///
268 CAddonAEStream(AudioEngineFormat format, unsigned int options = 0)
269 : m_kodiBase(::kodi::addon::CAddonBase::m_interface->toKodi->kodiBase),
270 m_cb(::kodi::addon::CAddonBase::m_interface->toKodi->kodi_audioengine)
271 {
272 AEStreamHandle *streamHandle = m_cb->MakeStream(m_kodiBase, format, options);
273 if (streamHandle == nullptr)
274 {
275 kodi::Log(ADDON_LOG_FATAL, "CAddonAEStream: MakeStream failed!");
276 }
277 }
278 //--------------------------------------------------------------------------
279
280 ~CAddonAEStream()
281 {
282 if (m_StreamHandle)
283 {
284 m_cb->FreeStream(m_kodiBase, m_StreamHandle);
285 m_StreamHandle = nullptr;
286 }
287 }
288
289 //==========================================================================
290 /// @ingroup cpp_kodi_audioengine_CAddonAEStream
291 /// @brief Returns the amount of space available in the stream
292 ///
293 /// @return The number of bytes AddData will consume
294 ///
295 unsigned int GetSpace()
296 {
297 return m_cb->AEStream_GetSpace(m_kodiBase, m_StreamHandle);
298 }
299 //--------------------------------------------------------------------------
300
301 //==========================================================================
302 /// @ingroup cpp_kodi_audioengine_CAddonAEStream
303 /// @brief Add planar or interleaved PCM data to the stream
304 ///
305 /// @param data array of pointers to the planes
306 /// @param offset to frame in frames
307 /// @param frames number of frames
308 /// @return The number of frames consumed
309 ///
310 unsigned int AddData(uint8_t* const *data, unsigned int offset, unsigned int frames)
311 {
312 return m_cb->AEStream_AddData(m_kodiBase, m_StreamHandle, data, offset, frames);
313 }
314 //--------------------------------------------------------------------------
315
316 //==========================================================================
317 /// @ingroup cpp_kodi_audioengine_CAddonAEStream
318 /// @brief Returns the time in seconds that it will take for the next added
319 /// packet to be heard from the speakers.
320 ///
321 /// @return seconds
322 ///
323 double GetDelay()
324 {
325 return m_cb->AEStream_GetDelay(m_kodiBase, m_StreamHandle);
326 }
327 //--------------------------------------------------------------------------
328
329 //==========================================================================
330 /// @ingroup cpp_kodi_audioengine_CAddonAEStream
331 /// @brief Returns if the stream is buffering
332 ///
333 /// @return True if the stream is buffering
334 ///
335 bool IsBuffering()
336 {
337 return m_cb->AEStream_IsBuffering(m_kodiBase, m_StreamHandle);
338 }
339 //--------------------------------------------------------------------------
340
341 //==========================================================================
342 /// @ingroup cpp_kodi_audioengine_CAddonAEStream
343 /// @brief Returns the time in seconds of the stream's cached audio samples.
344 /// Engine buffers excluded.
345 ///
346 /// @return seconds
347 ///
348 double GetCacheTime()
349 {
350 return m_cb->AEStream_GetCacheTime(m_kodiBase, m_StreamHandle);
351 }
352 //--------------------------------------------------------------------------
353
354 //==========================================================================
355 /// @ingroup cpp_kodi_audioengine_CAddonAEStream
356 /// @brief Returns the total time in seconds of the cache
357 ///
358 /// @return seconds
359 ///
360 double GetCacheTotal()
361 {
362 return m_cb->AEStream_GetCacheTotal(m_kodiBase, m_StreamHandle);
363 }
364 //--------------------------------------------------------------------------
365
366 //==========================================================================
367 /// @ingroup cpp_kodi_audioengine_CAddonAEStream
368 /// @brief Pauses the stream playback
369 ///
370 void Pause()
371 {
372 return m_cb->AEStream_Pause(m_kodiBase, m_StreamHandle);
373 }
374 //--------------------------------------------------------------------------
375
376 //==========================================================================
377 /// @ingroup cpp_kodi_audioengine_CAddonAEStream
378 /// @brief Resumes the stream after pausing
379 ///
380 void Resume()
381 {
382 return m_cb->AEStream_Resume(m_kodiBase, m_StreamHandle);
383 }
384 //--------------------------------------------------------------------------
385
386 //==========================================================================
387 /// @ingroup cpp_kodi_audioengine_CAddonAEStream
388 /// @brief Start draining the stream
389 ///
390 /// @param[in] wait [opt] Wait until drain is finished if set to
391 /// true, otherwise it returns direct
392 ///
393 /// @note Once called AddData will not consume more data.
394 ///
395 void Drain(bool wait)
396 {
397 return m_cb->AEStream_Drain(m_kodiBase, m_StreamHandle, wait=true);
398 }
399 //--------------------------------------------------------------------------
400
401 //==========================================================================
402 /// @ingroup cpp_kodi_audioengine_CAddonAEStream
403 /// @brief Returns true if the is stream draining
404 ///
405 bool IsDraining()
406 {
407 return m_cb->AEStream_IsDraining(m_kodiBase, m_StreamHandle);
408 }
409 //--------------------------------------------------------------------------
410
411 //==========================================================================
412 /// @ingroup cpp_kodi_audioengine_CAddonAEStream
413 /// @brief Returns true if the is stream has finished draining
414 ///
415 bool IsDrained()
416 {
417 return m_cb->AEStream_IsDrained(m_kodiBase, m_StreamHandle);
418 }
419 //--------------------------------------------------------------------------
420
421 //==========================================================================
422 /// @ingroup cpp_kodi_audioengine_CAddonAEStream
423 /// @brief Flush all buffers dropping the audio data
424 ///
425 void Flush()
426 {
427 return m_cb->AEStream_Flush(m_kodiBase, m_StreamHandle);
428 }
429 //--------------------------------------------------------------------------
430
431 //==========================================================================
432 /// @ingroup cpp_kodi_audioengine_CAddonAEStream
433 /// @brief Return the stream's current volume level
434 ///
435 /// @return The volume level between 0.0 and 1.0
436 ///
437 float GetVolume()
438 {
439 return m_cb->AEStream_GetVolume(m_kodiBase, m_StreamHandle);
440 }
441 //--------------------------------------------------------------------------
442
443 //==========================================================================
444 /// @ingroup cpp_kodi_audioengine_CAddonAEStream
445 /// @brief Set the stream's volume level
446 ///
447 /// @param[in] volume The new volume level between 0.0 and 1.0
448 ///
449 void SetVolume(float volume)
450 {
451 return m_cb->AEStream_SetVolume(m_kodiBase, m_StreamHandle, volume);
452 }
453 //--------------------------------------------------------------------------
454
455 //==========================================================================
456 /// @ingroup cpp_kodi_audioengine_CAddonAEStream
457 /// @brief Gets the stream's volume amplification in linear units.
458 ///
459 /// @return The volume amplification factor between 1.0 and 1000.0
460 ///
461 float GetAmplification()
462 {
463 return m_cb->AEStream_GetAmplification(m_kodiBase, m_StreamHandle);
464 }
465 //--------------------------------------------------------------------------
466
467 //==========================================================================
468 /// @ingroup cpp_kodi_audioengine_CAddonAEStream
469 /// @brief Sets the stream's volume amplification in linear units.
470 ///
471 /// @param[in] amplify The volume amplification factor between
472 /// 1.0 and 1000.0
473 ///
474 void SetAmplification(float amplify)
475 {
476 return m_cb->AEStream_SetAmplification(m_kodiBase, m_StreamHandle, amplify);
477 }
478 //--------------------------------------------------------------------------
479
480 //==========================================================================
481 /// @ingroup cpp_kodi_audioengine_CAddonAEStream
482 /// @brief Returns the size of one audio frame in bytes (channelCount * resolution)
483 ///
484 /// @return The size in bytes of one frame
485 ///
486 const unsigned int GetFrameSize() const
487 {
488 return m_cb->AEStream_GetFrameSize(m_kodiBase, m_StreamHandle);
489 }
490 //--------------------------------------------------------------------------
491
492 //==========================================================================
493 /// @ingroup cpp_kodi_audioengine_CAddonAEStream
494 /// @brief Returns the number of channels the stream is configured to accept
495 ///
496 /// @return The channel count
497 ///
498 const unsigned int GetChannelCount() const
499 {
500 return m_cb->AEStream_GetChannelCount(m_kodiBase, m_StreamHandle);
501 }
502 //--------------------------------------------------------------------------
503
504 //==========================================================================
505 /// @ingroup cpp_kodi_audioengine_CAddonAEStream
506 /// @brief Returns the stream's sample rate, if the stream is using a dynamic
507 /// sample rate, this value will NOT reflect any changes made by calls to
508 /// SetResampleRatio()
509 ///
510 /// @return The stream's sample rate (eg, 48000)
511 ///
512 const unsigned int GetSampleRate() const
513 {
514 return m_cb->AEStream_GetSampleRate(m_kodiBase, m_StreamHandle);
515 }
516 //--------------------------------------------------------------------------
517
518 //==========================================================================
519 /// @ingroup cpp_kodi_audioengine_CAddonAEStream
520 /// @brief Return the data format the stream has been configured with
521 ///
522 /// @return The stream's data format (eg, AUDIOENGINE_FMT_S16LE)
523 ///
524 const AEDataFormat GetDataFormat() const
525 {
526 return m_cb->AEStream_GetDataFormat(m_kodiBase, m_StreamHandle);
527 }
528 //--------------------------------------------------------------------------
529
530 //==========================================================================
531 /// @ingroup cpp_kodi_audioengine_CAddonAEStream
532 /// @brief Return the resample ratio
533 ///
534 /// @note This will return an undefined value if the stream is not resampling
535 ///
536 /// @return the current resample ratio or undefined if the stream is not resampling
537 ///
538 double GetResampleRatio()
539 {
540 return m_cb->AEStream_GetResampleRatio(m_kodiBase, m_StreamHandle);
541 }
542 //--------------------------------------------------------------------------
543
544 //==========================================================================
545 /// @ingroup cpp_kodi_audioengine_CAddonAEStream
546 /// @brief Sets the resample ratio
547 ///
548 /// @note This function may return false if the stream is not resampling, if
549 /// you wish to use this be sure to set the AESTREAM_FORCE_RESAMPLE option
550 ///
551 /// @param[in] ratio the new sample rate ratio, calculated by
552 /// ((double)desiredRate / (double)GetSampleRate())
553 ///
554 void SetResampleRatio(double ratio)
555 {
556 m_cb->AEStream_SetResampleRatio(m_kodiBase, m_StreamHandle, ratio);
557 }
558 //--------------------------------------------------------------------------
559
560 private:
561 void* m_kodiBase;
562 AddonToKodiFuncTable_kodi_audioengine* m_cb;
563 AEStreamHandle *m_StreamHandle;
564 };
565
566 //============================================================================
567 /// @ingroup cpp_kodi_audioengine
568 /// @brief Get the current sink data format
569 ///
570 /// @param[in] format Current sink data format. For more details see AudioEngineFormat.
571 /// @return Returns true on success, else false.
572 ///
573 inline bool GetCurrentSinkFormat(AudioEngineFormat &format)
574 {
575 return ::kodi::addon::CAddonBase::m_interface->toKodi->kodi_audioengine->GetCurrentSinkFormat(::kodi::addon::CAddonBase::m_interface->toKodi->kodiBase, &format);
576 }
577 //----------------------------------------------------------------------------
578
579} /* audioengine */
580} /* kodi */