In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to use SoundTouch to change the sound and convert to wav format for playback under iOS. Xiaobian thinks it is quite practical, so share it with you as a reference. I hope you can gain something after reading this article.
The first problem is: we call the microphone of the phone to collect the recording data is pcm format, pcm is naked data, no header information, general players can not play, so we generally have to convert to wav format, so ordinary players can play, and in iOS you can directly use AVAudioPlayer to play, without having to call the underlying API. About if pcm to wav, you can refer to my previous blog post, here is no longer introduced.
The second question is: How do I change my voice? Fortunately, there is an open source software called "soundtouch", which is written in c++, so it is easy to integrate into iOS, and the acclaimed Tom Cat uses "soundtouch" to change its voice. Here's how to compile soundtouch in iOS:
1. Download the source code of soundtouch at http://www.surina.net/soundtouch/sourcecode.html Download it as a compressed package. The directory structure after decompression is as follows:
2. Copy all the.h and.cpp files and place them in the soundtouch directory:
3. By default, there is no soundtouch_config.h header file, because it is related to the specific platform and needs to be compiled manually. You can compile according to the soundtouch documentation, if you don't want to compile yourself, you can use my compiled.
4. Then add all the files under soundtouch to xcode, because it is a c++ file, so modify the corresponding.m file to a.mm file to support c++ compilation.
5. By default, the recording data used by soundtouch is float type, but our recording data is generally short type, so find the STTypes.h header file and comment out the sentence #define SOUNDTOUCH_FLOAT_SAMPLES 1.
#define SOUNDTOUCH_INTEGER_SAMPLES 1 opens as follows:
6. Also soundtouch and iOS both typedef BOOL
soundtouch:typedef int BOOL
iOS: typedef signed char BOOL
If there is a conflict when compiling this way, change the soundtouch to typedef signed char BOOL.
7. To change the sound, just use a header file, soundtouch.h, import it into your file, create a soundtouch object, and set some parameters:
These are just parameters I set myself and can be adjusted according to my own needs.
8. Call the mSoundTouch.putSamples method to pass the recording data to the soundtouch process with two parameters; the first is the recording data, the short * type, and the second is the length of the recording data. If your recording data is char * type, you need to cast it, for example:
char *pcmData = (char *)audioData.bytes;
int pcmSize = audioData.length;
int nSamples = pcmSize / 2;
//here forced char * to short *, note that the length is the original general, because a short is equivalent to 2 char
mSoundTouch.putSamples((short *)pcmData, nSamples);
9. Call receiveSamples to receive the processed data from soundtouch. This method also has two parameters, which are buffers for storing data. Therefore, we have to create a buffer to receive data in advance. The return value of this function is the actual received size. This method should be called in a loop. When receiveSamples returns 0, it indicates that the reception is complete, and exits the loop. Otherwise, it continues to receive. For example:
short *samples = newshort[pcmSize];
int numSamples = 0;
do {
memset(samples, 0, pcmSize);
numSamples = mSoundTouch.receiveSamples(samples, pcmSize);
[soundTouchDatas appendBytes:samples length:numSamples*2];
} while (numSamples > 0);
delete [] samples;
[audioData release];
In my example, the received data is stored in NSMutable. Since NSMutable is stored in bytes, the size is multiplied by 2, that is, numSamples*2.
10. After the recording data is changed, you need to add a 44-byte header before it, convert it to wav format, and then save it.
//add 44 bytes of wav header
NSMutableData *wavDatas = [[NSMutableDataalloc] init];
int fileLength = soundTouchDatas.length;
void *header = createWaveHeader(fileLength, 1, 16000, 16);
[wavDatas appendBytes:header length:44];
[wavDatas appendData:soundTouchDatas];
//Save to Documents directory
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [path stringByAppendingPathComponent:@"soundtouch.wav"];
[wavDatas writeToFile:filePath atomically:YES];
[soundTouchDatas release];
[wavDatas release];
The above is the main step of using soundtouch sound change, the attachment is a complete project, you can run directly.
Note: this project can only run on real phones, and must be armv7 after the phone.
Operating instructions:
1. Click "Start talking" to record
2. Click "Done" to stop recording
3. When the recording stops, it will automatically play back, and then return to "Start talking"
About "How to use SoundTouch to change voice and convert to wav format for playback under iOS" This article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.