How to Implement audio synthesis in Android

Source: Internet
Author: User
Before talking about this topic, you need to know the following points:

1. mediarecorder/audiorecord differences
Mediarecorder is a common recording class provided by the system, which can be used to generate Audio Recording files. Audiorecord is also a recording class provided by the system, but it can directly capture audio streams and developers can process their contents in real time. Common scenarios such as voice chat, tomcat, and karaoke.
2. Riff/WAV/PCM/raw/MP3
Riff is a file description format. The WAV file uses the riff description. The first 44 bytes are the riff description content. PCM is the metadata of media data and directly records the sound content. Wav = riff + PCM, raw = PCM, while MP3 is a compression encoding, which roughly means compressing PCM through algorithms.
3. Sampling Rate, bit rate, number of channels, sample value
The sampling rate is the acquisition frequency of analog signals (the number of samples per second). The higher the sampling rate, the more authentic the sound is. The bit rate is the sampling rate after the analog signal is converted into a digital signal. The sampling value is also called the number of digits per sample, common 8-bit and 16-bit channels are single-channel and dual-channel channels. A single channel occupies 8/16 bits per sample, a dual channel occupies 16/32 bits per sample, and a height of 8/16 bits indicates a left-channel, the lower 8/16 bits indicates the right audio channel;

Then we will discuss how to implement audio synthesis:

1. First, we need to obtain the user's voice and the recording class. to synchronize and process data, we can only use audiorecord.
2. The recording sound is PCM, that is, metadata. We need to merge it with another sound file so that we must decode another file as PCM data.
3. After obtaining the PCM Data of the recording and accompaniment, the hybrid algorithm is used to calculate the average value of each byte.
4. Obtain the mixed audio PCM and then encode it. For example, to generate a WAV format, you only need to add a 44-byte riff description before PCM data.

Problems encountered during implementation:

1. recording distortion: During the test, the playback speed of the recording file was found to be very fast. The analysis showed that the wav file header description does not match the actual content of the file. For example, I used the 44100 sampling rate, single channel, the 16-bit sampling value is recorded, but the two channels are written in riff. Therefore, the calculated playback time is half the actual time, so the playback speed is doubled. The conclusion is that the description of riff must match the actual content of PCM. Otherwise, the playing sound may be abnormal. ---- It can also be inferred that as long as the file can be played, but the playing effect is abnormal, it means that most of the results are caused by mismatch of the riff and PCM types.

2. Some files are not supported: The probability of occurrence is very small, which may be due to the file encoding problem.

3. Recording Duration: audiorecord. Read blocks time consumption. Therefore, the mediaplayer is played completely and only half of the recording is recorded.

4. Hybrid Algorithm: theoretically, the hybrid algorithm adds each byte by dividing the number of digits and calculates the average value. However, the precondition is that the number of channels and the sample value of the two files must be the same. We have also explained the functions of the number of channels and the sample value before, so we can infer that if the sample value is 8 bits and 16 bits, we need to convert byte to short and then overlay the average number; if a single channel needs to be combined with a dual channel, the write algorithm is added to the first half or the second half. The dual channel to a single channel overwrites the first half and the second half, calculates the average, and then overwrites the single channel to calculate the average.

5. The Android device can only record the audio in the form of a single channel. Even if the input dual-channel parameters do not report an error, the generated PCM can only be processed as a single channel file.

======================================== Welfare ============== source code = ======================================

Android audiorecord recording demo

Http://download.csdn.net/detail/h3c4lenovo/4684011

Android audio mixing-Karaoke Synthesis

Http://download.csdn.net/detail/h3c4lenovo/4684013

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.