audio-io.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #pragma once
  15. #include "../util/c99defs.h"
  16. #include "media-io.h"
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /*
  21. * Base audio output component. Use this to create an audio output track
  22. * for the media.
  23. */
  24. struct audio_output;
  25. struct audio_line;
  26. typedef struct audio_output *audio_t;
  27. typedef struct audio_line *audio_line_t;
  28. enum audio_format {
  29. AUDIO_FORMAT_UNKNOWN,
  30. AUDIO_FORMAT_U8BIT,
  31. AUDIO_FORMAT_16BIT,
  32. AUDIO_FORMAT_32BIT,
  33. AUDIO_FORMAT_FLOAT,
  34. };
  35. enum speaker_layout {
  36. SPEAKERS_UNKNOWN,
  37. SPEAKERS_MONO,
  38. SPEAKERS_STEREO,
  39. SPEAKERS_2POINT1,
  40. SPEAKERS_QUAD,
  41. SPEAKERS_4POINT1,
  42. SPEAKERS_5POINT1,
  43. SPEAKERS_5POINT1_SURROUND,
  44. SPEAKERS_7POINT1,
  45. SPEAKERS_7POINT1_SURROUND,
  46. SPEAKERS_SURROUND,
  47. };
  48. struct audio_data {
  49. const void *data;
  50. uint32_t frames;
  51. uint64_t timestamp;
  52. };
  53. struct audio_info {
  54. const char *name;
  55. const char *format;
  56. uint32_t samples_per_sec;
  57. enum audio_format type;
  58. enum speaker_layout speakers;
  59. };
  60. static inline uint32_t get_audio_channels(enum speaker_layout speakers)
  61. {
  62. switch (speakers) {
  63. case SPEAKERS_MONO: return 1;
  64. case SPEAKERS_STEREO: return 2;
  65. case SPEAKERS_2POINT1: return 3;
  66. case SPEAKERS_SURROUND:
  67. case SPEAKERS_QUAD: return 4;
  68. case SPEAKERS_4POINT1: return 5;
  69. case SPEAKERS_5POINT1:
  70. case SPEAKERS_5POINT1_SURROUND: return 6;
  71. case SPEAKERS_7POINT1: return 8;
  72. case SPEAKERS_7POINT1_SURROUND: return 8;
  73. case SPEAKERS_UNKNOWN: return 0;
  74. }
  75. return 0;
  76. }
  77. static inline size_t get_audio_bytes_per_channel(enum audio_format type)
  78. {
  79. switch (type) {
  80. case AUDIO_FORMAT_U8BIT: return 1;
  81. case AUDIO_FORMAT_16BIT: return 2;
  82. case AUDIO_FORMAT_FLOAT:
  83. case AUDIO_FORMAT_32BIT: return 4;
  84. case AUDIO_FORMAT_UNKNOWN: return 0;
  85. }
  86. return 0;
  87. }
  88. static inline size_t get_audio_size(enum audio_format type,
  89. enum speaker_layout speakers, uint32_t frames)
  90. {
  91. return get_audio_channels(speakers) *
  92. get_audio_bytes_per_channel(type) *
  93. frames;
  94. }
  95. #define AUDIO_OUTPUT_SUCCESS 0
  96. #define AUDIO_OUTPUT_INVALIDPARAM -1
  97. #define AUDIO_OUTPUT_FAIL -2
  98. EXPORT int audio_output_open(audio_t *audio, media_t media,
  99. struct audio_info *info);
  100. EXPORT audio_line_t audio_output_createline(audio_t audio);
  101. EXPORT size_t audio_output_blocksize(audio_t audio);
  102. EXPORT const struct audio_info *audio_output_getinfo(audio_t audio);
  103. EXPORT void audio_output_close(audio_t audio);
  104. EXPORT void audio_line_destroy(audio_line_t line);
  105. EXPORT void audio_line_output(audio_line_t line, const struct audio_data *data);
  106. #ifdef __cplusplus
  107. }
  108. #endif