Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. #import "PlayPCM.h"
  2. AudioFileID audioFile;
  3. SInt64 inStartingPacket = 0;
  4. AudioQueueRef audioQueue;
  5.  
  6. @implementation PlayPCM
  7.  
  8.  
  9. void AudioOutputCallback(
  10. void* inUserData,
  11. AudioQueueRef outAQ,
  12. AudioQueueBufferRef outBuffer)
  13. {
  14.  
  15. AudioStreamPacketDescription* packetDescs;
  16.  
  17. UInt32 bytesRead;
  18. UInt32 numPackets = 8000;
  19. OSStatus status;
  20. status = AudioFileReadPackets(audioFile,
  21. false,
  22. &bytesRead,
  23. packetDescs,
  24. inStartingPacket,
  25. &numPackets,
  26. outBuffer->mAudioData);
  27.  
  28.  
  29. if(numPackets)
  30. {
  31. outBuffer->mAudioDataByteSize = bytesRead;
  32. status = AudioQueueEnqueueBuffer(audioQueue,
  33. outBuffer,
  34. 0,
  35. packetDescs);
  36.  
  37. inStartingPacket += numPackets;
  38. }
  39. else
  40. {
  41. NSLog(@"number of packets = null ") ;
  42. AudioQueueFreeBuffer(audioQueue, outBuffer);
  43. }
  44.  
  45. }
  46.  
  47. -(id)init{
  48. if (self = [super init]) {
  49.  
  50. }
  51. return self;
  52. }
  53. - (void)setupAudioFormat
  54. {
  55.  
  56. NSLog(@"setting format");
  57. format.mFormatID = kAudioFormatLinearPCM;
  58. format.mSampleRate = 44100;
  59. format.mFramesPerPacket = 1;
  60. format.mChannelsPerFrame = 1;
  61. format.mBytesPerFrame = 2;
  62. format.mBytesPerPacket = 2;
  63. format.mBitsPerChannel = 16;
  64. format.mFormatFlags = kLinearPCMFormatFlagIsBigEndian |
  65. kLinearPCMFormatFlagIsSignedInteger |
  66. kLinearPCMFormatFlagIsPacked;
  67. }
  68.  
  69. - (void)startPlayback
  70. {
  71.  
  72. int counter = 0;
  73. [self setupAudioFormat];
  74. OSStatus status;
  75. NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
  76. NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@ "test1.wav"];
  77. NSLog(@"file path = %@",filePath);
  78. //fUrl = [NSURL URLWithPath:@"file:///Users/Inscripts/Desktop/test1.wav"];
  79. fUrl = [NSURL fileURLWithPath:filePath];
  80. //CFURLRef fileURL = (__bridge CFURLRef)(fUrl);
  81. CFURLRef fileURL = CFURLCreateWithString(NULL, (CFStringRef) filePath, NULL);
  82. status = AudioFileOpenURL(fileURL, kAudioFileReadPermission, 0,&audioFile);
  83. NSLog(@"file opening status = %d",(int)status);
  84. if(status == 0)
  85. { NSLog(@"file opened");
  86. status = AudioQueueNewOutput(&(format),
  87. AudioOutputCallback,
  88. (__bridge void *)(self),
  89. CFRunLoopGetCurrent(),
  90. kCFRunLoopCommonModes,
  91. 0,
  92. &audioQueue);
  93. NSLog(@"audio queue create status = %d",(int)status);
  94.  
  95. if(status == 0)
  96. {
  97.  
  98.  
  99. AudioQueueAllocateBuffer(audioQueue, 1600000, &audioQueueBuffer);
  100. AudioOutputCallback((__bridge void *)(self), audioQueue, audioQueueBuffer);
  101. [self performSelector:@selector(startQueue) withObject:self afterDelay:50];
  102.  
  103. }
  104. }
  105.  
  106. if(status != 0)
  107. {
  108. NSLog(@"failed");
  109. // labelStatus.text = @"Play failed";
  110. }
  111. }
  112.  
  113. -(void)startQueue{
  114. NSLog(@"start queue called");
  115. OSStatus status = AudioQueueStart(audioQueue, NULL);
  116. if(status == 0)
  117. {
  118. NSLog(@"ok");
  119.  
  120. // labelStatus.text = @"Playing";
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement