SuperpoweredIOSAudioIO.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #import <AVFoundation/AVAudioSession.h>
  2. /**
  3. @brief Output channel mapping.
  4. This structure maps the channels you provide in the audio processing callback to the appropriate output channels.
  5. You can have multi-channel (more than a single stereo channel) output if a HDMI or USB audio device is connected. iOS does not provide multi-channel output for other audio devices, such as wireless audio accessories.
  6. @em Example:
  7. Let's say you have four output channels, and you'd like the first stereo pair on USB 3+4, and the other stereo pair on the iPad's headphone socket.
  8. 1. Set deviceChannels[0] to 2, deviceChannels[1] to 3.
  9. 2. Set USBChannels[2] to 0, USBChannels[3] to 1.
  10. @em Explanation:
  11. - Your four output channels are having the identifiers: 0, 1, 2, 3.
  12. - Every iOS device has just one stereo built-in output. This is represented by deviceChannels, and (1.) sets your second stereo pair (2, 3) to these.
  13. - You other stereo pair (0, 1) is mapped to USBChannels. USBChannels[2] represents the third USB channel.
  14. @since Multi-channel output is available in iOS 6.0 and later.
  15. @param deviceChannels The iOS device's built-in output channels.
  16. @param HDMIChannels HDMI output channels.
  17. @param USBChannels USB output channels.
  18. @param numberOfHDMIChannelsAvailable Number of available HDMI output channels. Read only.
  19. @param numberOfUSBChannelsAvailable Number of available USB output channels. Read only.
  20. @param headphoneAvailable Something is plugged into the iOS device's headphone socket or not. Read only.
  21. */
  22. typedef struct multiOutputChannelMap {
  23. // Information you provide:
  24. int deviceChannels[2];
  25. int HDMIChannels[8];
  26. int USBChannels[32];
  27. // READ ONLY information:
  28. int numberOfHDMIChannelsAvailable, numberOfUSBChannelsAvailable;
  29. bool headphoneAvailable;
  30. } multiOutputChannelMap;
  31. /**
  32. @brief Input channel mapping.
  33. Similar to the output channels, you can map the input channels too. It works with USB only.
  34. Let's say you set the channel count to 4, so RemoteIO provides 4 input channel buffers. Using this struct, you can map which USB input channel appears on the specific buffer positions.
  35. @since Available in iOS 6.0 and later.
  36. @see @c multiOutputChannelMap
  37. @param USBChannels Example: set USBChannels[0] to 3, to receive the input of the third USB channel on the first buffer.
  38. @param numberOfUSBChannelsAvailable Number of USB input channels.
  39. */
  40. typedef struct multiInputChannelMap {
  41. int USBChannels[32]; // You provide this.
  42. int numberOfUSBChannelsAvailable; // READ ONLY
  43. } multiInputChannelMap;
  44. @protocol SuperpoweredIOSAudioIODelegate;
  45. /**
  46. @brief You can have an audio processing callback with the delegate (Objective-C) or pure C. This is the pure C prototype.
  47. @return Return false for no audio output (silence).
  48. @param clientData A custom pointer your callback receives.
  49. @param buffers Input-output buffers.
  50. @param inputChannels The number of input channels.
  51. @param outputChannels The number of output channels.
  52. @param numberOfSamples The number of samples requested.
  53. @param samplerate The current sample rate in Hz.
  54. @param hostTime A mach timestamp, indicates when this chunk of audio will be passed to the audio output.
  55. */
  56. typedef bool (*audioProcessingCallback_C) (void *clientdata, float **buffers, unsigned int inputChannels, unsigned int outputChannels, unsigned int numberOfSamples, unsigned int samplerate, uint64_t hostTime);
  57. /**
  58. @brief Handles all audio session, audio lifecycle (interruptions), output, buffer size, samplerate and routing headaches.
  59. @warning All methods and setters should be called on the main thread only!
  60. */
  61. @interface SuperpoweredIOSAudioIO: NSObject {
  62. int preferredBufferSizeMs;
  63. bool saveBatteryInBackground;
  64. }
  65. /** @brief The preferred buffer size. Recommended: 12. */
  66. @property (nonatomic, assign) int preferredBufferSizeMs;
  67. /** @brief Save battery if output is silence and the app runs in background mode. True by default. */
  68. @property (nonatomic, assign) bool saveBatteryInBackground;
  69. /**
  70. @brief Creates the audio output instance.
  71. @param delegate The object fully implementing the SuperpoweredIOSAudioIODelegate protocol. Not retained.
  72. @param preferredBufferSize The initial value for preferredBufferSizeMs. 12 is good for every iOS device (512 samples).
  73. @param preferredMinimumSamplerate The preferred minimum sample rate. 44100 or 48000 are recommended for good sound quality.
  74. @param audioSessionCategory The audio session category. Audio input is enabled for the appropriate categories only!
  75. @param channels The number of channels in the audio processing callback.
  76. */
  77. - (id)initWithDelegate:(id<SuperpoweredIOSAudioIODelegate>)delegate preferredBufferSize:(unsigned int)preferredBufferSize preferredMinimumSamplerate:(unsigned int)preferredMinimumSamplerate audioSessionCategory:(NSString *)audioSessionCategory channels:(int)channels;
  78. /**
  79. @brief Starts audio processing.
  80. @return True if successful, false if failed.
  81. */
  82. - (bool)start;
  83. /**
  84. @brief Stops audio processing.
  85. */
  86. - (void)stop;
  87. /**
  88. @brief Call this to re-configure the channel mapping.
  89. */
  90. - (void)mapChannels;
  91. /**
  92. @brief Set the audio processing callback to a C function, instead of the delegate's Objective-C method.
  93. 99% of all audio apps work great with the Objective-C method, so you don't need to use this. Don't call this method after [start]!
  94. @param callback The callback function.
  95. @param clientdata Some custom pointer for the C processing callback. You can set it to NULL.
  96. */
  97. - (void)setProcessingCallback_C:(audioProcessingCallback_C)callback clientdata:(void *)clientdata;
  98. - (void)reconfigureWithAudioSessionCategory:(NSString *)audioSessionCategory;
  99. - (BOOL)getRuning;
  100. - (void)onRouteChange:(NSNotification *)notification;
  101. - (void)setdelg:(id<SuperpoweredIOSAudioIODelegate>)itdelegate;
  102. @end
  103. /**
  104. @brief You must implement this protocol to make SuperpoweredIOSAudioIO work.
  105. */
  106. @protocol SuperpoweredIOSAudioIODelegate
  107. /**
  108. @brief The audio session may be interrupted by a phone call, etc. This method is called on the main thread when this happens.
  109. */
  110. - (void)interruptionStarted;
  111. /**
  112. @brief The audio session may be interrupted by a phone call, etc. This method is called on the main thread when audio resumes.
  113. */
  114. - (void)interruptionEnded;
  115. /**
  116. @brief Called if the user did not grant a recording permission for the app.
  117. */
  118. - (void)recordPermissionRefused;
  119. /**
  120. @brief This method is called on the main thread, when a multi-channel audio device is connected or disconnected.
  121. @param outputMap Map the output channels here.
  122. @param inputMap Map the input channels here.
  123. @param externalAudioDeviceName The name of the attached audio device, such as the model of the sound card.
  124. @param outputsAndInputs A human readable description about the available outputs and inputs.
  125. */
  126. - (void)mapChannels:(multiOutputChannelMap *)outputMap inputMap:(multiInputChannelMap *)inputMap externalAudioDeviceName:(NSString *)externalAudioDeviceName outputsAndInputs:(NSString *)outputsAndInputs;
  127. /**
  128. @brief Process audio here.
  129. @return Return false for no audio output (silence).
  130. @param buffers Input-output buffers.
  131. @param inputChannels The number of input channels.
  132. @param outputChannels The number of output channels.
  133. @param numberOfSamples The number of samples requested.
  134. @param samplerate The current sample rate in Hz.
  135. @param hostTime A mach timestamp, indicates when this chunk of audio will be passed to the audio output.
  136. @warning It's called on a high priority real-time audio thread, so please take care of blocking and processing time to prevent audio dropouts.
  137. */
  138. - (bool)audioProcessingCallback:(float **)buffers inputChannels:(unsigned int)inputChannels outputChannels:(unsigned int)outputChannels numberOfSamples:(unsigned int)numberOfSamples samplerate:(unsigned int)samplerate hostTime:(UInt64)hostTime;
  139. @end