Also said Alsa music playback (mp3 file from scratch)

Source: Internet
Author: User
Tags fread int size

The first and most important is: I talked big, this article is a train of thought just, if you are music playing small white also want to understand the principle can look down, otherwise, return-1;

The existing music format is very different, so there is a ffmpeg saying that this framework is really good, audio and video transcoding are not alone, I am a dead brain, met do not understand the always want to understand, for music playback feeling is confused, after a few days of research after the general want to understand, It takes a few steps to play out a MP3 file: 1. transcoding, in fact, is to extract useful audio information in the file
2. wav waveform files obtained through the first step
3. Call the sound card to play WAV files, Linux 3.0 after the kernel is generally ALSA drive, windows for WAV is also a natural support


About the transcoding can see the lady wrote the transcoding series tutorial http://lfp001.iteye.com/blog/739585 transcoding command:
Ffmepg-i 1.mp3-f wav 1.wav

Compiling: Gcc-o alsa alsa.c-lasound

Play command

./alsa 1.wav

ALSA.C writer unknown, pure reference #include <stdio.h> #include <stdlib.h> #include <string.h> #include <alsa/    asoundlib.h> struct Wav_header {char rld[4];   
    riff symbol int Rlen;    Char wld[4];    Format type (wave) char fld[4];   "FMT" int flen;   sizeof (wave format Matex) short wFormatTag;    encoding format short wchannels;  Number of channels int nsamplespersec; Sampling frequency int navgbitspersample;//wave File sample size short wblockalign;   Block snap to short wbitspersample;        Wave file Sample size char dld[4];  "Data" int wsamplelength;

The size of the audio data} Wav_header;

int Set_pcm_play (FILE *fp);
        int main (int argc,char *argv[]) {if (argc!=2) {printf ("Usage:wav-player+wav file name\n");
    Exit (1);
    int nread;
    FILE *FP;
    Fp=fopen (Argv[1], "RB");
        if (fp==null) {perror ("Open File failed:\n");
    Exit (1);
    } nread=fread (&wav_header,1,sizeof (Wav_header), FP); printf ("nread=%d\n", nreAD);
    printf ("RIFF sign%s\n", Wav_header.rld);
    printf ("File size rlen:%d\n", wav_header.rlen);
    printf ("wld=%s\n", wav_header.wld);
    
   printf ("fld=%s\n", WAV_HEADER.FLD);
    
    printf ("flen=%d\n", Wav_header.flen);
    printf ("wformattag=%d\n", Wav_header.wformattag);
    printf ("Number of channels:%d\n", wav_header.wchannels);
    printf ("Sample frequency:%d\n", wav_header.nsamplespersec);
    printf ("navgbitspersample=%d\n", wav_header.navgbitspersample);
    printf ("wblockalign=%d\n", wav_header.wblockalign);
    
   printf ("Number of bits sampled:%d\n", wav_header.wbitspersample);
    printf ("data=%s\n", WAV_HEADER.DLD);
    
    
    
    
    
    printf ("wsamplelength=%d\n", wav_header.wsamplelength);
    Set_pcm_play (FP);
return 0;
        int Set_pcm_play (FILE *fp) {int rc;
        int ret;
        int size;        Snd_pcm_t* handle;
        PCI device handle snd_pcm_hw_params_t* params;//hardware information and PCM stream configuration unsigned int val;
       int dir=0; snd_pcm_uframes_t frames;
        Char *buffer;
        int channels=wav_header.wchannels;
        int frequency=wav_header.nsamplespersec;
        int bit=wav_header.wbitspersample;
        int datablock=wav_header.wblockalign;  unsigned char ch[100];
        Used to store the header information for WAV files Rc=snd_pcm_open (&handle, "Default", Snd_pcm_stream_playback, 0);
                if (rc<0) {perror ("\nopen PCM device failed:");
        Exit (1); } snd_pcm_hw_params_alloca (¶MS);
                ALLOCATE params structure body if (rc<0) {perror ("\nsnd_pcm_hw_params_alloca:");
        Exit (1); } rc=snd_pcm_hw_params_any (handle, params);//Initialize params if (rc<0) {perror ("\nsn
                D_pcm_hw_params_any: ");
        Exit (1);                                 } rc=snd_pcm_hw_params_set_access (handle, params, snd_pcm_access_rw_interleaved);
 Initialize access permission if (rc<0)       {perror ("\nsed_pcm_hw_set_access:");

        Exit (1); //Sample digit switch (BIT/8) {case 1:snd_pcm_hw_params_set_format (handle, params, snd_pcm_form
                AT_U8);
        break;
                Case 2:snd_pcm_hw_params_set_format (handle, params, snd_pcm_format_s16_le);
        break;
                Case 3:snd_pcm_hw_params_set_format (handle, params, snd_pcm_format_s24_le);

        break;  } rc=snd_pcm_hw_params_set_channels (handle, params, channels);
                Set the channel, 1 for single sound > channel, 2 for Stereo if (rc<0) {perror ("\nsnd_pcm_hw_params_set_channels:");
        Exit (1);
        } val = frequency;  Rc=snd_pcm_hw_params_set_rate_near (handle, params, &val, &dir);
                Set > Frequency if (rc<0) {perror ("\nsnd_pcm_hw_params_set_rate_near:");
        Exit (1);
        rc = Snd_pcm_hw_params (handle, params); If(rc<0)
        {perror ("\nsnd_pcm_hw_params:");
        Exit (1);  } rc=snd_pcm_hw_params_get_period_size (params, &frames, &dir);
                /* Get cycle length */if (rc<0) {perror ("\nsnd_pcm_hw_params_get_period_size:");
        Exit (1);   Size = frames * DATABLOCK;
    /*4 represents the data fast-length/buffer = (char*) malloc (size);  Fseek (Fp,58,seek_set);
                Locate the song to the data area while (1) {memset (buffer,0,sizeof (buffer));
                RET = fread (buffer, 1, size, FP);
                        if (ret = 0) {printf ("Song write end \ n");
                Break else if (ret!= size) {}//write audio data to PCM device WHI  
                 Le (ret = Snd_pcm_writei (handle, buffer, frames) <0) {usleep (2000); if (ret = =-epipe) {/* Epipe Means underrun * * * fprintf (stderr, "underrun occurred\n");
                 Complete the hardware parameter setting so that the equipment is ready for snd_pcm_prepare (handle);
                      else if (Ret < 0) {fprintf (stderr,
                 "Error from Writei:%s\n", Snd_strerror (ret));
        }} snd_pcm_drain (handle);
        Snd_pcm_close (handle);
        Free (buffer);
return 0; }



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.