jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
5.8KB

  1. /*
  2. Copyright (C) 2009 Grame
  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 2 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, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #include "JackAudioQueueAdapter.h"
  16. #include <CoreServices/CoreServices.h>
  17. namespace Jack
  18. {
  19. // NOT YET WORKING....
  20. static void Print4CharCode(char* msg, long c)
  21. {
  22. UInt32 __4CC_number = (c);
  23. char __4CC_string[5];
  24. *((SInt32*)__4CC_string) = EndianU32_NtoB(__4CC_number);
  25. __4CC_string[4] = 0;
  26. //printf("%s'%s'\n", (msg), __4CC_string);
  27. snprintf(__4CC_string, 5, "%s'%s'\n", (msg), __4CC_string);
  28. }
  29. void JackAudioQueueAdapter::CaptureCallback(void * inUserData,
  30. AudioQueueRef inAQ,
  31. AudioQueueBufferRef inBuffer,
  32. const AudioTimeStamp * inStartTime,
  33. UInt32 inNumPackets,
  34. const AudioStreamPacketDescription *inPacketDesc)
  35. {
  36. JackAudioQueueAdapter* adapter = (JackAudioQueueAdapter*)inUserData;
  37. printf("JackAudioQueueAdapter::CaptureCallback\n");
  38. if (AudioQueueEnqueueBuffer(adapter->fCaptureQueue, inBuffer, 0, NULL) != noErr) {
  39. printf("JackAudioQueueAdapter::CaptureCallback error\n");
  40. }
  41. // Use the adapter to communicate with audio callback
  42. // jack_adapter_push_input(adapter, audio_output, audio_output_buffer);
  43. }
  44. void JackAudioQueueAdapter::PlaybackCallback(void * inUserData,
  45. AudioQueueRef inAQ,
  46. AudioQueueBufferRef inCompleteAQBuffer)
  47. {
  48. JackAudioQueueAdapter* adapter = (JackAudioQueueAdapter*)inUserData;
  49. printf("JackAudioQueueAdapter::PlaybackCallback\n");
  50. if (AudioQueueEnqueueBuffer(adapter->fPlaybackQueue, inCompleteAQBuffer, 0, &adapter->fPlaybackPacketDescs) != noErr) {
  51. printf("JackAudioQueueAdapter::PlaybackCallback error\n");
  52. }
  53. // Use the adapter to communicate with audio callback
  54. // jack_adapter_pull_output(adapter, audio_input, audio_input_buffer);
  55. }
  56. JackAudioQueueAdapter::JackAudioQueueAdapter(int inchan, int outchan, jack_nframes_t buffer_size, jack_nframes_t sample_rate, jack_adapter_t* adapter)
  57. :fCaptureChannels(inchan), fPlaybackChannels(outchan), fBufferSize(buffer_size), fSampleRate(sample_rate), fAdapter(adapter)
  58. {}
  59. JackAudioQueueAdapter::~JackAudioQueueAdapter()
  60. {}
  61. int JackAudioQueueAdapter::Open()
  62. {
  63. OSStatus err;
  64. AudioStreamBasicDescription captureDataFormat;
  65. /*
  66. captureDataFormat.mSampleRate = fSampleRate;
  67. captureDataFormat.mFormatID = kAudioFormatLinearPCM;
  68. //captureDataFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
  69. captureDataFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked;
  70. captureDataFormat.mBytesPerPacket = sizeof(float);
  71. captureDataFormat.mFramesPerPacket = 1;
  72. captureDataFormat.mBytesPerFrame = sizeof(float);
  73. captureDataFormat.mChannelsPerFrame = fCaptureChannels;
  74. captureDataFormat.mBitsPerChannel = 32;
  75. */
  76. captureDataFormat.mSampleRate = fSampleRate;
  77. captureDataFormat.mFormatID = kAudioFormatLinearPCM;
  78. captureDataFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
  79. captureDataFormat.mBytesPerPacket = 4;
  80. captureDataFormat.mFramesPerPacket = 1; // this means each packet in the AQ has two samples, one for each channel -> 4 bytes/frame/packet
  81. captureDataFormat.mBytesPerFrame = 4;
  82. captureDataFormat.mChannelsPerFrame = 2;
  83. captureDataFormat.mBitsPerChannel = 16;
  84. if ((err = AudioQueueNewInput(&captureDataFormat, CaptureCallback, this, CFRunLoopGetCurrent(), kCFRunLoopCommonModes, 0, &fCaptureQueue)) != noErr) {
  85. Print4CharCode("error code : unknown", err);
  86. return -1;
  87. }
  88. //AudioQueueSetProperty(fCaptureQueue, kAudioQueueProperty_MagicCookie, cookie, size)
  89. //AudioQueueSetProperty(fCaptureQueue, kAudioQueueProperty_ChannelLayout, acl, size
  90. AudioStreamBasicDescription playbackDataFormat;
  91. playbackDataFormat.mSampleRate = fSampleRate;
  92. playbackDataFormat.mFormatID = kAudioFormatLinearPCM;
  93. playbackDataFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
  94. playbackDataFormat.mBytesPerPacket = sizeof(float);
  95. playbackDataFormat.mFramesPerPacket = 1;
  96. playbackDataFormat.mBytesPerFrame = sizeof(float);
  97. playbackDataFormat.mChannelsPerFrame = fPlaybackChannels;
  98. playbackDataFormat.mBitsPerChannel = 32;
  99. if ((err = AudioQueueNewOutput(&playbackDataFormat, PlaybackCallback, this, CFRunLoopGetCurrent(), kCFRunLoopCommonModes, 0, &fPlaybackQueue)) != noErr) {
  100. Print4CharCode("error code : unknown", err);
  101. return -1;
  102. }
  103. //AudioQueueSetProperty(fPlaybackQueue, kAudioQueueProperty_MagicCookie, cookie, size);
  104. //AudioQueueSetProperty(fPlaybackQueue, kAudioQueueProperty_ChannelLayout, acl, size);
  105. //AudioQueueSetParameter(fPlaybackQueue, kAudioQueueParam_Volume, volume
  106. //AudioQueueStart(fCaptureQueue, NULL);
  107. AudioQueueStart(fPlaybackQueue, NULL);
  108. return 0;
  109. }
  110. int JackAudioQueueAdapter::Close()
  111. {
  112. AudioQueueStop(fCaptureQueue, true);
  113. AudioQueueStop(fPlaybackQueue, true);
  114. AudioQueueDispose(fCaptureQueue, true);
  115. AudioQueueDispose(fPlaybackQueue, true);
  116. return 0;
  117. }
  118. int JackAudioQueueAdapter::SetSampleRate(jack_nframes_t sample_rate)
  119. {
  120. return 0;
  121. }
  122. int JackAudioQueueAdapter::SetBufferSize(jack_nframes_t buffer_size)
  123. {
  124. return 0;
  125. }
  126. };