Mobile Internet combat--mobile audio and graphics optimization processing

Source: Internet
Author: User
Tags string format

Objective:
Mobile applications, need to save traffic (bandwidth), large resource packs on the user experience is harmful. As a result, mobile development requires thin resources (audio/picture), but also guaranteed audio/picture quality. This article focuses on how to optimize processing resources (audio/picture), and how to compromise and trade-offs between high compression ratios and high quality (quality/quality). This article involves two chunks, one for speech processing and the other for image processing.

Note: This article is mainly for mobile developers, using programming to optimize processing. doped with small (Mumuxinfei) immature views and views, hoping to stimulate.

*) Sample Explanation:
1. Constructing the application scenario
Convert audio files in normal WAV format into smaller MP3 format audio files.
The sample code is implemented with Jave (Java Audio Video Encoder). Please click: Jave official website.

File Source = new file ("Data/source.wav"); File target = new file ("Data/target.mp3"),//*) sets the audio properties of audioattributes = new Audioattributes (); Audio.setcodec (" Libmp3lame "); audio.setbitrate (New Integer (128000)); Audio.setchannels (New Integer (2)); Audio.setsamplingrate (new Integer (44100));//*) decoder set encodingattributes attrs = new Encodingattributes (); Attrs.setformat ("MP3"); Attrs.setaudioattributes (audio); Encoder Encoder = new Encoder (); Encoder.encode (source, target, attrs);

2. Effect display
After converting the WAV format to MP3 format, the actual size comparison is as follows:

WAV format files: 100044byte, Mp3 format files: 18839byte
After a small series of hands-on experience (audition), stand firm expression of no obvious difference in sound quality, but the file compression ratio has a significant increase.
3. Puzzled and puzzled
Although the big success, but the small part still some puzzled, doubt a lump tuo. -_-!!!
#) What exactly is the bitrate (bitrate), channel (channel), sample rate (samplingrate) in the audio file?
#) in file compression and sound quality, that element plays a more important role? Is he, is she, or is it? Alas, my mother, small made me silly to divide.
Small hand splash, so modify the code, take different bit rate/sample rate value, to see if there are surprises will appear?

*) Set the audio properties Audioattributes audios = new Audioattributes (); Audio.setcodec ("Libmp3lame");//audio.setbitrate (New Integer (128000)); Audio.setbitrate (New Integer (250000)), Audio.setchannels (New Integer (2)),//audio.setsamplingrate (New Integer (44100 )); Audio.setsamplingrate (new Integer (40000));

I have a big rub, "surprise" appeared,-_-!!!!!!! Actually strikes, also throws the unusual, throws, throws, throws, throws your sister Ah ...

Exception in thread ' main ' it.sauronsoftware.jave.EncoderException:Error while opening codec for output stream #0.0-may Be incorrect parameters such as bit_rate, rate, width or height

The small part cannot help but puzzled, the bit rate and the sampling rate have some kind of mysterious relation? You see, he both ended up with a ' rate ' word, and it was so catchy? Do not (MD, accidentally uttered the truth)? Forget, in fact, small part of the heart or a little look forward to, oh yeah.
and doubts continue ....
#) is there a limit to the value of bit rate and sample rate?
#) How does bit rate and sample rate exist ?

*) Basic knowledge
Really curiosity killed the cat, small make up with these doubts to know (posture) yuan (abundant) Bo (Rich) Baidu June.

Small: Baidu June, Hello, you can give us a description of the audio file in the bit rate (bitrate), channel (channel), sampling rate (samplingrate) What is the specific?
Baidu June: #%[email protected]#$%^&* () ~& (@!*^[email protected]&*^*&% (*%&^%^$~!*) () *) @& (*^@# (*^ ( !& (!^ (

Xiao Bian (anger): Speaking ...
Baidu June:
Audio files have three important attribute descriptions: Sample rate (samplingrate)/bit rate (bitrate)/channel (channel)
Bit rate: How many bits of audio data are needed per second, the higher the bitrate, the better the audio quality, and the larger the file size.
#) Sample rate: The sample rate defines the number of samples per second that are extracted from a continuous signal and composed of discrete signals, expressed in hertz (Hz).
#) Channel: Mono channel, two channels, mono is mixed evenly from the left and right channels, while the two channels are from different sound
From the left and right channels out and synthesis, the scene of strong three-dimensional.
Small (inner): Hum, Baidu June really eat soft afraid hard, but seems to say a good professional, unknown feel li.

Small series: Then ask the bit rate and sampling rate is the value of the limit?
Baidu June:
#) Bit Rate value
Bit rate value: 32kbps/96kbps/128kbps/192kbps/224kbps/320kbps
#) The value of the sample rate
44.1kHz Series Sampling: 11.025kHz, 22.05kHz, 44.1kHz, 88.2kHz, 176.4kHz
48kHz Series Sampling: 12kHz, 24kHz, 48kHz, 96kHz, 192kHz
Small (inner): This is a long insight, later called you still throw, throw, not to leave your sister, Wahaha, hello, say you? Look at what you see,!!!!

Small: Then ask, this 44.1kHz has a special reason?
Baidu June: Human auditory band in 20Hz to 20kHz, by the Nyquist sampling theorem, sampling rate as long as more than twice times the signal bandwidth will not produce aliasing. In fact, the music CD specification uses 44.1kHz (20kHz twice times more) as the sampling standard. So the 44.1kHz has a certain correlation with the human auditory band.

Exciting time, finally came, at this time the air seems to have solidified ....

Small series (HI): That bit rate June and sampling rate June what is the affair? (Ah yuck, accidentally slipped the word ...)
Baidu June: Two dimensions, non-impact. In short, the bitrate is used in the audio encoding process while the sample rate is used for voice playback. The bitrate is proportional to the sound quality and the file size is inversely proportional to the sound quality.

The sky is a crow flying, whoa Wah ...

Small (inner): This TM is playing with me? Make so affectionate, also has been inseparable, finally unexpectedly told me, they do not have a half gross money relationship, this who believe AH!!!!!!
Small (lost): really an unexpected result, never thought, this world still have pure friendship exists, whether you believe it or not, anyway I believe.

Alas, suddenly feel the chrysanthemum itch ...

*) Advanced article
1). Review Audio Collection meta information
How to do this with Jave, the code snippet is as follows:

Multimediainfo info = encoder.getinfo (target); SYSTEM.OUT.PRINTLN (info);

The Console result output is as follows:

(Decoder=pcm_s16le, samplingrate=22050, channels=2, bitrate=705)

Commentary: Decoder: pcm_s16le, sample rate: 22.05kHz, dual channel, bit rate 705Kbps
2). Try to convert WAV to the MP3 format for each bit rate

Sample rate/Bit rate 24kbps 48kbps 96kbps 192kbps
11.025kHz 3792 7554 15077 --
22.05kHz 3636 7241 14451 --
44.1kHz -- 7084 14137 28243

Commentary: Compared to horizontal/vertical, you can see that the file size is related to bit rate, while the sample rate and bit rate have a certain combination of qualification.

3). Try converting WA to the audio format of each decoder
The audio decoder, depending on the audio format, is simply listed below:

mp3 = Libmp3lameogg = Vorbiswav = PCM_S16BE/PCM_S16LE/PCM_S24LE3GP = LIBFAAC

For more decoders, please click on: official website
In the same condition (bit rate: 24kbps, sample rate: 22.05kHz, dual channel)

Audio format mp3 Ogg Wav 3gp
File size 3636 6586 100044 2668

When in the same condition (bit rate: 48kbps, sample rate: 44.1kHz, dual channel)

Audio format mp3 Ogg Wav 3gp
File size 7084 8722 199980 5177

Commentary: As can be seen, the file size under the same conditions: 3GP < MP3 < OGG << wav, but the file compression ratio is not good, and ultimately need to be in the compression ratio/sound quality to take a compromise

Image processing
Mobile-to-image format selection: Mainstream for png/jpeg format. PNG it combines the quality of the JPG image with the transparency of the GIF format, with a larger file size than JPEG. JPEG format has good quality, high color and saturation, relatively small files. Therefore, there are generally the following practical principles: small size, small number of colors or use to transparent when using PNG, large size, color gradient with more color when using JPG.
The picture optimization of the mobile side includes two aspects, one for the picture format conversion and one for the picture pixel size adjustment.
Code snippet:

The public void Convertimage (string sourcefile, string destfile, int newwidth, int newheight) {//*) assumes that the incoming destfile is *.jpg,  *.png's naming method, this side does not do check processing String format = destfile.substring (Destfile.lastindexof ('. ') + 1);    try {Image srcimage = imageio.read (new File (sourcefile));    *) Draw the source image to the target image bufferedimage Destimage = new BufferedImage (Newwidth, Newheight, BUFFEREDIMAGE.TYPE_3BYTE_BGR);    graphics2d g2 = (graphics2d) destimage.getgraphics ();    G2.drawimage (srcimage, 0, 0, newwidth, newheight, NULL);  *) Generate target image imageio.write (destimage, format, new File (DestFile));  } catch (IOException e) {e.printstacktrace (); }}

Commentary: This code can achieve jpg=>png, jpg=>gif, Jpg=>bmp, png=>jpg conversion, while achieving different resolution of the image conversion

Experimental comparison, the original image as follows (show welfare)


The file size ratio is as follows:

Picture format Jpg Png Gif Bmp
File size 19.0KB 267.9KB 48.7KB 386.8KB

Comparison can be found, the image file size is JPG < GIF < PNG < BMP under the same pixel

Continue the experiment, the large-resolution image into a small resolution (mainstream resolution) of the image, the compression rate of how many

Resolution 1600*1200 1280*1024 1080*960 960*540 854*480
File size 251.7KB 188.8KB 157.7KB 90.3KB 74.7KB

If you want to further control the quality of the image (for example, JPG image quality, default quality is 0.75):
The code snippet is as follows:

public void Convert2jpgimage (String sourcefile, string destfile, int newwidth,  int newheight, float quality) {fileoutputstream out = null;    try {Image srcimage = imageio.read (new File (sourcefile));    *) Draw the source image to the target image bufferedimage Destimage = new BufferedImage (Newwidth, Newheight, BUFFEREDIMAGE.TYPE_3BYTE_BGR);    graphics2d g2 = (graphics2d) destimage.getgraphics ();    G2.drawimage (srcimage, 0, 0, newwidth, newheight, NULL);    *) generate the target image, using Com.sun.image.codec.jpeg.JPEGImageEncoder to implement out = new FileOutputStream (destfile);    JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out);    JPEGEncodeParam Jep = Jpegcodec.getdefaultjpegencodeparam (destimage);    Jep.setquality (quality, true);  Encoder.encode (Destimage, Jep);      } catch (IOException e) {} finally {if (out! = null) {try {out.close (); } catch (IOException e) {}}}} 

Summarize:
Mobile development requires certain audio/image optimization techniques, which is especially important for resource-class apps. File compression ratio is not higher the better, the pursuit of high-quality/high-quality will pay a certain price. All in all: There is no perfect solution, only the eternal compromise.

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.