Design of MP3 Player Based on Embedded ARM

Source: Internet
Author: User
Tags oracle vm virtualbox vm virtualbox

I. design question: Arm-Based MP3 Player Design

Ii. Course Design Teaching Purpose:

1. Understand and master the general design methods of system circuits, and have preliminary independent design capabilities;

2. Master the principles of Unix/Linux systems;

3. Improve the ability to analyze and solve problems independently using the learned theoretical knowledge;

4. Master the development process of ARM technology.

Iii. design requirements

1. First, implement the basic functions of the MP3 player on the PC, including playing one or several MP3-format songs, and implement the fast return and fast forward functions.

2. debug and implement the above functions on the arm Development Board to complete the course design.

Iv. Basic Principles:

1. Introduction to embedded systems:

With the rapid development of microelectronics, computer and mobile communication technologies and the wide application of network technology in the past 20 years, the actual situation has undergone fundamental changes. For example, the personal computer PC evolved from a microcomputer defined at the end of 1970s processes much faster than the definition of large, medium, and small computers. With the extensive penetration of computer technology into other industries and the combination of application technologies in other industries, the application-centric classification method seems to be more practical; computer embedded applications and non-embedded applications are divided into general computer and embedded computer. General Computer has the basic standard form of general computer. By assembling different application software, general computer appears in basically the same face and applies it to all aspects of society. Its typical product is PC; non-General Computer Computers-embedded computers are non-general computer applications. They are hidden in various devices, devices, products, and systems as the core components of embedded systems. Therefore, embedded computers are a form of computer existence in the development of computer technology and are separated from the development of computer technology.

2. Introduction to Linux:

Linux is a free and open-source Unix-like operating system. There are many different Linux systems, but they all use the Linux kernel. Linux can be installed on a variety of computer hardware devices, from mobile phones, tablets, routers and video game consoles to desktop computers, mainframes, and supercomputers. Linux is a leading operating system. The world's top 10 super computers run Linux operating systems. Strictly speaking, the term "Linux" only represents the Linux kernel, but in fact people are used to using Linux to describe the entire Linux kernel-based operating system and use various GNU engineering tools and databases.

3. MP3 audio format:

MP3 is short for MPEG-1 audio Layer 3, which is a popular Digital Audio Encoding and lossy compression format. MP3 technology should be used to greatly reduce the space required for audio file storage. It discards the data that is not important to human hearing in the pulse-Coded Modulation (PCM) audio data, thus achieving a high compression ratio (up to-10 ). Simply put, MP3 performs spectrum analysis on audio files during encoding, filters out noise levels, and then quantifies the remaining parts of each bit, at last, an MP3 file with a high compression ratio is formed, and the compressed file can achieve a relatively close effect to the original audio source during playback.

V. Solution selection:

1. Selection of embedded system development methods:

Nowadays, there are two development modes for embedded systems. One is based on ARM chips, which is similar to the one developed by single-chip microcomputer.

 

Mode, that is, directly programming on the hardware, which is obviously unreasonable, so it is difficult to effectively develop embedded application systems that can handle complex problems. The second is the development mode based on the embedded operating system. In the development process of embedded systems, cross-platform development is generally adopted. That is, programming on a general PC, and then compiling and linking the source program into a binary code format image that can run on the target platform. Finally, place the image to a specific location on the target platform, and start the code (bootloader) on the target board to execute the two-line code to run the embedded system. Currently, almost all embedded development adopts the second development mode.

Therefore, our curriculum design naturally adopts the second development mode.

2. determine the development process:

We fully follow the embedded development process in the enterprise, first build a Linux operating system development environment on the PC. Second, build a Linux compiling and execution environment on the Development Board. Then write the program on the PC and perform cross-compilation to execute the program on the PC. Finally, download the program to the Development Board to complete the design. The entire development process is as follows:

 

6. Build a Linux operating system development platform on a PC

1. Install the Linux Virtual Machine Oracle VM virtualbox:

This course is designed with the kernel of linuxdebian, 2.6.26-2-686. First, run the Virtual Machine installation program. After installation, create a new Linux operating system and enter the user name and password to enter the operating system to write the program. This is the first step.

2. Familiar with Linux Command Line operations and Common commands:

(1) file-related commands:

PWD: view the current directory

Ls: View subdirectories in the current directory

Ls-l file name: view detailed information of the specified file

Ls-A: View information about all objects including hidden objects

CD: Switch Directories

CD/: switch to the root directory

CD... : Switch to the upper-level directory

Mkdir file name: Create a folder

RM file name: delete a file

Rm-r file name: delete the file (cannot be recovered)

Touch file name: New File

Cat file name: view the File Content

Echo "content"> file name: Write the content to the end of the file, without overwriting the original content

Cat file 1> file 2: copy the content of file 1 to file 2

(2) Common commands used in programming:

Vim "file name": Open the Editor similar to Windows notepad and name it "File Name". After the compiler is opened, the default mode is command mode and cannot be entered. Press the "I" key to enter the input mode. You can edit the code. Press the "ESC" key to return to the command mode. Press the ":" key to enter the last line mode.

W file name: in Vim's last line mode, save the file as the specified file name.

Q: Exit Vim In the last-line mode of vim.

WQ: Save and exit the file in Vim's last line mode.

DD: deletes the entire row.

NDD: delete n rows

DW: delete a word

Ndw: delete n words

D $: Delete content from the cursor to the end

Y: Copy

Nyy: Copy n rows

P: Paste

? Or/content to be searched: Find the specified content

% S // G: after entering the last line mode, replace all the searched content

U: Undo this action

CTRL + R: restore to the previous state

Set nu: Open the row number in last line mode.

Set Nonu: undo the row number in last row Mode

GCC file name: Compile the specified file and output the executable file a. Out.

GCC file name 1-o file name 2: Compile the specified file and output the executable file "file name 2"

./File Name: Execute the specified file

Vim makefile: Compile makefile and collectively compile the file

Output Executable File 1: source file 1

GCC source file 1-O Executable File 1

Output executable file 2: source file 2

GCC source file 2-O executable file 2

......

Make: Execute makefile

MAN 2 function name: View function User Manual

(3) install the Sound Card Driver

Step 1: CD/lib/modules/2.6.26-2-686/kernel/sound/CORE/OSS/

Step 2: sudomodprobe snd-PCM-OSS

At this point, the sound card driver has been installed

7. Compile the source program


1. First, you can play audio files in WAV format. The program flowchart is as follows:



The source program is as follows:

#include<stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include<stdlib.h>#include <sys/ioctl.h>#include <sys/soundcard.h>#include <termios.h>#include <unistd.h>int kbhit(void){struct termios oldt, newt;int ch;int oldf;tcgetattr(STDIN_FILENO, &oldt);newt = oldt;newt.c_lflag &= ~(ICANON | ECHO);tcsetattr(STDIN_FILENO, TCSANOW,&newt);oldf = fcntl(STDIN_FILENO, F_GETFL, 0);fcntl(STDIN_FILENO, F_SETFL, oldf |O_NONBLOCK);ch = getchar();tcsetattr(STDIN_FILENO, TCSANOW,&oldt);fcntl(STDIN_FILENO, F_SETFL, oldf);if(ch != EOF)   {    ungetc(ch, stdin);  return 1;   } return 0;}int main(int argc,char **argv){   char *buf=malloc(1024);   int fd=open(argv[1],O_RDONLY|O_CREAT|O_APPEND,0666);   int fd1=open(argv[2],O_WRONLY|O_CREAT|O_APPEND,0666);   printf("%d,%d\n",fd,fd1);   int du=1;   while(du>0)   {          while(kbhit())       {       switch(getchar())           {           case '1':lseek(fd,1000000,SEEK_CUR);break;           case '2':lseek(fd,-1000000,SEEK_CUR);break;           default:break;           }       break;       }       du=read(fd,buf,50);       int fmt=AFMT_S16_LE,channels=2,speed=44100;       ioctl(fd1,SNDCTL_DSP_SETFMT,&fmt);       ioctl(fd1,SNDCTL_DSP_CHANNELS,&channels);       ioctl(fd1,SNDCTL_DSP_SPEED,&speed);       int xie=write(fd1,buf,du);       printf("%d %d ",du,xie);    }   close(fd);   close(fd1);return 0;}

At this point, audio files in WAV format are played.

 

2. Enable MP3 audio files to be played. Add the MP3 decoding program on the basis of the above and make improvements:

#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdio.h>#include <sys/soundcard.h>#include <termios.h>#include <mad.h>struct mad_stream decode_stream;struct mad_frame decode_frame;struct mad_synth decode_synth;static inline signed int scale(mad_fixed_tsample){     sample+= (1L << (MAD_F_FRACBITS - 16));    if(sample >= MAD_F_ONE)            sample = MAD_F_ONE - 1;    else if(sample < -MAD_F_ONE)            sample = -MAD_F_ONE;    return sample >> (MAD_F_FRACBITS + 1 - 16);  } int mad_init(void){     mad_stream_init(&decode_stream);       mad_synth_init(&decode_synth);       mad_frame_init(&decode_frame);       return0;}int change_input_mode (void){     intret;       structtermios new_opts;       if(tcgetattr(0, &new_opts)<0){              printf ("error: tcgetattr()");              return 1;}       new_opts.c_lflag&= ~(ICANON);       new_opts.c_cc[VMIN]=0;       new_opts.c_cc[VTIME]=0;       if(tcsetattr(0,TCSANOW, &new_opts)<0){              printf ("error: tcsetattr()");              return 1; }       return0;}int main(int argc,char **argv){       intfd,snd;       charsnddata[32768];       charinput_buf[5*1024*1024]={0};       structstat st;       inttotal=0;       intret;       intsize=4*1024*1024;       intret_val;       mad_init();       change_input_mode();       fd=open("../nuannuan.mp3",O_RDONLY);       if(fd==-1){              printf("open audio fileerror\n");              return -1;}       snd=open("/dev/dsp",O_WRONLY);       if(snd==-1){              printf("open dsp error\n");              return -1;}       read(fd,input_buf,size);       mad_stream_buffer(&decode_stream,input_buf, size);       while(1){              ret_val =mad_frame_decode(&decode_frame, &decode_stream);              printf("%d\n",ret_val);          if (ret_val == 0) {                     mad_synth_frame(&decode_synth,&decode_frame);                     intfmt=AFMT_S16_LE;                     ret=ioctl(snd,SNDCTL_DSP_SETFMT,&fmt);                     intchannel=decode_synth.pcm.channels;                     ret=ioctl(snd,SNDCTL_DSP_CHANNELS,&channel);                     intsamplerate=decode_synth.pcm.samplerate;                     ret=ioctl(snd,SNDCTL_DSP_SPEED,&samplerate);                     unsignedchar *tmp=snddata;                     unsignedint size=decode_synth.pcm.length;                     inti=0;                     while(i<size){                            unsigned int sample;                            sample=scale(decode_synth.pcm.samples[0][i]);                            *tmp++=sample>>0;                            *tmp++=sample>>8;                            if(decode_synth.pcm.channels==2){                                   sample=scale(decode_synth.pcm.samples[1][i]);                                   *tmp++=sample>>0;                                   *tmp++=sample>>8;                            }                            i++;                     }                     if(decode_synth.pcm.channels==2)                            size=size*4;                     else                            size=size*2;                            write(snd,snddata,size);     }   }       close(fd);       close(snd);       return0;}

Here, the MP3 player software is designed.

8. Compile the source program and link the executable files to the arm Development Board.

IX. Conclusion

Secondly, this course design also increases my ability to analyze and solve problems. For example, how to find materials to learn new things and solve problems that have never been encountered before. In this course design process, the teacher just assigned the task of the day, explained the new knowledge used, and then completed the task by ourselves and compiled the program. This process has developed my ability to think independently and laid the foundation for my future work in this area.

During the course design process, I also encountered many problems. For example, when writing a program to write the content in one file to another, you will always find that the read content is incorrect. After careful check and debugging, the reason is that the cache variable does not have an initial value. How to Implement the fast forward and return functions, find that it cannot solve the problem of playing music and check the external buttons. After searching for information on the Internet, we finally find a way to use the kbhit () function. There are other problems. In the end, after thinking or asking a teacher, you will be able to solve them.

Finally, I am very happy to complete my course design and feel full for the many things I have learned in this course design. In general, I have learned a lot in this course design, and I have gained a lot. First of all, through this course design, I really learned about the ultra-cutting-edge embedded discipline and a typical embedded application-MP3. In the course design process, I personally experienced the development process of the embedded system and fully realized how the embedded system implements such complex functions. This gives you a brand new outlook on your future work and provides great encouragement to you.

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.