[Reprinted] [corrected] using the SoundTouch library in IOS for sound changes

Source: Internet
Author: User

I have previously written an article "using the SoundTouch library in IOS to achieve sound changes ".

Finally, many of my friends posted on their blog that the code can run on the simulator and cannot run on a real machine. They have been busy some time ago, 2013 the rankings of good-looking American TV series do not have time to go through the code and check it out.

Today, I checked the root cause of the problem and passed the test on IPhone 4 (ios5.0.1). I will update some code below.

 

It is also Apple's official SpeakHere. To download the code, please refer to the previous article.

  1. Void AQRecorder: StartRecord (CFStringRef inRecordFile)
  2. {
  3. MSoundTouch. setSampleRate (44100); // mRecordFormat. mSampleRate
  4. MSoundTouch. setChannels (1); // mRecordFormat. mChannelsPerFrame
  5. MSoundTouch. setTempoChange (1.0 );
  6. MSoundTouch. setPitchSemiTones (9 );
  7. MSoundTouch. setratechanges (-0.7 );
  8.  
  9. MSoundTouch. setSetting (SETTING_SEQUENCE_MS, 40 );
  10. MSoundTouch. setSetting (SETTING_SEEKWINDOW_MS, 16 );
  11. MSoundTouch. setSetting (SETTING_OVERLAP_MS, 8 );
  12. // Only use one of the following two options
  13. // MSoundTouch. setSetting (SETTING_USE_QUICKSEEK, 0 );
  14. // MSoundTouch. setSetting (SETTING_USE_AA_FILTER ,! (0 ));
  15. // MSoundTouch. setSetting (SETTING_AA_FILTER_LENGTH, 32 );

 

The two lines in red are very important, and the sampling rate and channel need to be specified. If you need to comment out two variables later, you must execute the SetupAudioFormat method. Otherwise, it is invalid because everyone understands it.

Update the audio callback function captured by the microphone according to the code.

  1. //____________________________________________________________________________________
  2. // AudioQueue callback function, called when an input buffers has been filled.
  3. Void AQRecorder: MyInputBufferHandler (void * inUserData,
  4. AudioQueueRef inAQ,
  5. AudioQueueBufferRef inBuffer,
  6. Const AudioTimeStamp * inStartTime,
  7. UInt32 inNumPackets,
  8. Const AudioStreamPacketDescription * inPacketDesc)
  9. {
  10. AQRecorder * aqr = (AQRecorder *) inUserData;
  11. Try {
  12. If (inNumPackets> 0 ){
  13. UInt32 audioDataByteSize = inBuffer-> mAudioDataByteSize;
  14. CAStreamBasicDescription queueFormat = aqr-> DataFormat ();
  15. SoundTouch * soundTouch = aqr-> GetSoundTouch ();
  16.  
  17. Uint nSamples = audioDataByteSize/queueFormat. mBytesPerPacket;
  18. SoundTouch-> putSamples (const SAMPLETYPE *) inBuffer-> mAudioData, nSamples );
  19. SAMPLETYPE * samples = (SAMPLETYPE *) malloc (audioDataByteSize );
  20. UInt32 numSamples;
  21. Do {
  22. Memset (samples, 0, audioDataByteSize );
  23. NumSamples = soundTouch-> receiveSamples (SAMPLETYPE *) samples, nSamples );
  24. // Write packets to file
  25. XThrowIfError (AudioFileWritePackets (aqr-> mRecordFile,
  26. FALSE,
  27. NumSamples * queueFormat. mBytesPerPacket,
  28. NULL,
  29. Aqr-> mRecordPacket,
  30. & NumSamples,
  31. Samples ),
  32. "AudioFileWritePackets failed ");
  33. Aqr-> mRecordPacket + = numSamples;
  34. } While (numSamples! = 0 );
  35. Free (samples );
  36. }
  37. // If we're not stopping, re-enqueue the buffe so that it gets filled again
  38. If (aqr-> IsRunning ())
  39. XThrowIfError (AudioQueueEnqueueBuffer (inAQ, inBuffer, 0, NULL), "AudioQueueEnqueueBuffer failed ");
  40. } Catch (CAXException e ){
  41. Char buf [256];
  42. Fprintf (stderr, "Error: % s (% s) \ n", e. mOperation, e. FormatError (buf ));
  43. }
  44. }

The reason should be clearly understood.

Previously, I wrote code on xcode 3. x. When a friend gave me feedback, I thought it was a version issue and the result was not a magic cinema.

The updated code is correct on xcode4.2 and iPhone 4. If you have any questions, take a closer look at the two articles. No. The parameter settings are incorrect.

Ps: QQ often not online, there is a problem on Weibo: http://weibo.com/yarin

This article is from the "yarin's blog" blog, please be sure to keep this http://yarin.blog.51cto.com/1130898/794768

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.