iOS Draw WAV waveform diagram

Source: Internet
Author: User

Recently studied how to draw a WAV waveform on iOS. Looked up a lot of information, did not find a very complete introduction, I here to summarize some experience.

First of all, we need to understand the 3 important indicators of WAV: sample rate, number of sample bits, number of channels. The following is illustrated in the example of 16KHz, 16Bit, Mono.

Sample rate: (also known as sampling speed or sampling frequency) defines the number of samples per second that are extracted from a continuous signal and composed of discrete signals, expressed in hertz (Hz). The inverse of the sampling frequency is the sampling period (also known as the sampling time), which represents the time interval between samples. The sampling rate is 16KHz, which indicates that there are 16K samples per second, i.e. 16 values are collected in 0.001 seconds.

Number of sample bits: That is, the sampled or sampled values, the parameters used to measure sound fluctuations, are the number of bits of the digital sound signal used by the sound card when collecting and playing sound files. The bit of the sound card objectively reflects the accuracy of the digital sound signal's description of the input sound signal. 16Bit indicates that a value is marked with a 16-bit (2-byte) computer.

Number of channels: refers to the number of speakers to support different sounds. The common ones are mono and two-channel.

Bit rate: The number of bits (bit) transmitted per second, equal to the sample rate * Sample bit number, in bps (bit per Second). Sample audio has a bit rate of 256kbps.

The C language does not provide a dedicated WAV audio file processing framework, so we can only analyze the waveform of WAV audio by reading the file's 2 binary values. This requires a certain understanding of the format of WAV audio.

WAV audio is divided into two parts: file header and data block.

Table 1 file headers for WAV files

Offset address

Number of bytes

Type

Content

00h~03h

4

Character

Resource Exchange File flag (RIFF) Note character case!

04h~07h

4

Long integers

The total number of bytes from the next address to the end of the file

08h~0bh

4

Character

WAV file flag (WAVE) Note the character case!

0ch~0fh

4

Character

Waveform format flag (FMT) Note the character case!

10h~13h

4

Integer

Filter bytes (typically 00000010H)

14h~15h

2

Integer

Type of format (value 1 o'clock, indicates data is linear PCM encoded)

16h~17h

2

Integer

Number of channels, mono is 1, dual sound is 2

18h~1bh

4

Long integers

Sampling frequency

1ch~1fh

4

Long integers

Waveform data transfer rate (average bytes per second)

20h~21h

2

Integer

Number of adjustments in data (in bytes)

22h~23h

2

Integer

Number of sample data bits

Table 2 data blocks for WAV sound files

Offset address

bytes

Type

Content

24h~27h

4

character

data marker ( information ) Note the case of characters!

28h~2bh

4

Long integer

Total sampled data

2CH ...

...

...

sampled data

Wave files are made up of several chunk. In the file where it appears, include the following:

RIFF WAVE Chunk, Position 00h~0bh.

Format Chunk, marked with ' FMT '. In general, size is 16, at which point the last additional information is not present, and if 18 is the last 2 bytes of additional information. The WAV format, mainly made up of some software, contains additional information of that 2 bytes. Position 0CH ~23h.

Fact Chunk, an optional field, typically contains the WAV file when it is converted by some software, including the Chunk.

Data Chunkis the place where WAV data is actually stored and ' data ' is used as the Chunk mark. Then the size of the data. This is followed by WAV data. Starting from 24H.

In addition to fact chunk, the other three chunk are required. Each chunk has its own ID, which is located at the beginning of chunk, and is marked as 4 bytes. And immediately after the ID is chunk large
Small (the number of other bytes remaining after removing the number of bytes of ID and size), 4 bytes, low bytes indicating low values, and high bytes indicating high values.

After understanding the structure of WAV file, we can easily remove the corresponding information of WAV. The next step is to draw the curve, and we use quartz to draw. Apple has provided a set of examples for reference

http://developer.apple.com/library/ios/#samplecode/quartzdemo/introduction/intro.html

The key is the Quartzview and quartzlines of the two files. In the quartzlines.m file, we can find the drawing curve method we want, which can be used as a reference.

The next thing is very simple, we only need to start from the position of 2CH, every 2 bytes (in relation to the number of sample bits) of the data, and then draw to the screen. Therefore, the number of audio data points is: The total number of sampled data/2. If you need to draw the image to the interface, the x-coordinate range 10~310,y The coordinate range 200~400, just make a map of the points.

Attention:

The source only considers cases where there is no fact chunk. Actually, when you read data, you should add a judgment.
if (strcmp (ID, "data")!=0) {//not EQ data
TODO Read Fact Chunk
The structure is 4 bytes of fact chunk data length +fact chunk data
}

---------------------------The above data is reproduced in the data---------------------------------//

Add two download connections later:

Reference example QUARTZLINES.M (to zoom in on Y for a better display effect)

http://download.csdn.net/detail/daiyelang/6617369

Demo

http://download.csdn.net/detail/daiyelang/6617383

iOS Draw WAV waveform diagram

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.