Phpanaly.wav file and draw a waveform in png format

Source: Internet
Author: User
Phpanaly.wave.png format waveforms are drawn and read from phpanaly.wav, and the waveforms in png format are drawn. the waveforms of audio files are analyzed and drawn using Php, which is rarely seen on the Internet. In fact, as long as you use Php fseek, fopen, fopen, pack/unpack functions, and powerful gd graphics libraries according to the wav file specifications, these are all very easy. "> <LINKhr

Using Php to analyze and draw the waveform of audio files is rarely seen on the Internet. In fact, it is easy to use Php fseek, fopen, fopen, pack/unpack functions, and powerful gd graphics library according to wav file specifications. Many may be unfamiliar with the pack/unpack functions. this is actually because Php uses perl and they provide methods to access complex binary data structures using scripting. My simplified program can only process RIFF audio files in PCM format (which is also the most common wav format), without restrictions, but the bit rate (BitsPerSample) should be 16.

Here are wave file format and MicroSoft wave soundfile format for reference. Here is an actual example (for more information, see downloading and downloading)

  

The following is a reference clip:

1 2

3 function wav_graph ($ file, $ f = 0, $ w = 0)

4 {

5 global $ DATA_DIR;

6

7 if (! Is_file ($ file) return 0;

8 $ fp = fopen ($ DATA_DIR. $ file, 'r ');

9 $ raw = fread ($ fp, 36 );

10 $ str = '';

11 $ header = unpack ('a4riff/VSize/A4Wav/A4Head/VHeadSize/vPCM/vChannels/VSampleRate/VByteRate/vBlockAlign/vSampleBits ', $ raw );

12 foreach ($ header as $ k => $ v)

13 $ str. = $ k. ':'. $ v .'';

14 fseek ($ fp, 36 + $ header ['headsize']-16 );

15 $ raw = fread ($ fp, 8 );

16 $ data = unpack ('a4data/VDataSize ', $ raw );

17 foreach ($ data as $ k => $ v)

18 $ str. = $ k. ':'. $ v .'';

19

20 $ B = $ header ['samples'];

21 $ c = $ header ['channels'];

22 $ l = $ B * $ c/8; // sample frame length in bytes

23 $ s = $ data ['datasize ']/$ l; // total number of samples

24 $ r = $ header ['samplerate'];

25 if ($ f) $ h = pow (2, $ B)/$ f;

26 else {$ h = 200; $ f = pow (2, $ B-1)/$ h ;}

27 if ($ w = 0) $ w = round ($ r/1000); // default to show 1 k sample frames per minute

28

29 header ("Content-type: image/png ");

30 $ im = imagecreate ($ s/$ w, $ h * $ c * 2 );

31 imagecolorallocate ($ im, 0xff, 0xff, 0xff); // white bg

32 $ color = imagecolorallocate ($ im, 0, 0,255); // black

33 // imagestring ($ im, 5, 5, 5, $ str, $ color );

34

35 $ x = 0; $ y = array (); $ yn = array ();

36 for ($ I = 0; $ I <$ c; $ I ++) $ y [$ I] = $ h * $ I + $ h;

37 $ n = $ l * $ w;

38 while (1)

39 {

40 if ($ s = 0) break;

41 if ($ s <$ n) $ n = $ s;

42 $ samples = fread ($ fp, 1000 * $ n );

43 if ($ samples === FALSE) break;

44 $ packed = unpack ("s *", $ samples );

45 foreach ($ packed as $ k => $ v)

46 {

47 $ cnt = ($ K-1) % ($ w * $ l );

48 if ($ cnt> $ c-1) continue;

49 $ yn [$ cnt] = $ h * $ cnt + $ h-$ v/$ f;

50 imageline ($ im, $ x, $ y [$ cnt], $ x + 1, $ yn [$ cnt], $ color );

51 $ y [$ cnt] = $ yn [$ cnt];

52 $ x ++;

53}

54 $ s-= $ n;

55}

56

57 imagepng ($ im );

58 imagedestroy ($ im );

59}

60

61 // wav_graph('audio2.wav ');

62?>
 

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.