⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client.cpp

📁 我写的基于2410的音频播放的程序
💻 CPP
字号:
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/soundcard.h>
#include <sys/ioctl.h>
#include <string.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h> 
#include "cirqueue.h"

// a Sample size define
const int frame_size = 1024;

// define server service port
const int SERV_PORT = 1080;

// define sound device name
#define DEVICE_NAME "/dev/audio"

// Circle Queue for Sampling and Send
CirQueue mQueue;

// Sample Sound
bool get_sample(int handle_sound, char * sample_buf)
{
    audio_buf_info info;
    
    // Judge whether can get Sample without block
    // Only read sample when has enough data, then return true
    // else return false
    ioctl(handle_sound, SNDCTL_DSP_GETISPACE, &info);
    if (info.bytes >= frame_size)
    {
        read(handle_sound, sample_buf, frame_size);
        return true;
    }
    else return false;
}

// Judge whether we can read sample data from sound card driver without blocked
bool HasSample(int sound_h, int read_size)
{
    audio_buf_info info;
    ioctl(sound_h, SNDCTL_DSP_GETISPACE, &info);
    if(info.bytes >= read_size)
        return true;
    else
        return false;
}

int main(int argc, char **argv)
{
    int sockfd;
    sockaddr_in servaddr;
    int soundfd;
    char soundbuf[frame_size];
    
    /* check args */
    if(argc != 3)
    {
        printf("usage: micphone <IPaddress> <Port>\n");
        exit(1);
    }

    /* init servaddr */
    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(SERV_PORT);
    if(inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
    {
        printf("[%s] is not a valid IPaddress\n", argv[1]);
        exit(1);
    }

    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    
    // Open Sound Card Device
    if((soundfd = open(DEVICE_NAME, O_RDWR, 0)) == -1)
    {   
        //close sound device
        //printf("fault is here!");
        //printf("[%s] is not a valid IPaddress\n","thisone");
        //close(soundfd);
        perror(DEVICE_NAME);
        close(sockfd);
        exit(-1);
    }
    
    // Reset Sound Card Device
    int i;
    i=0;
    ioctl (soundfd,SNDCTL_DSP_RESET,(char *)&i) ;

    // Disable Block Mode in Playing Mode or Recording Mode
    i=0;
    ioctl (soundfd,SNDCTL_DSP_SYNC,(char *)&i);
    
    // Enable NonBlock Mode of Sound Card Driver
    i=1;
    ioctl (soundfd,SNDCTL_DSP_NONBLOCK,(char *)&i);
    
    // Set Sampling Rate
    i=44100;
    ioctl (soundfd,SNDCTL_DSP_SPEED,(char *)&i);
    
    // Set Sound Channels Number to 1
    i=1;
    ioctl (soundfd,SNDCTL_DSP_CHANNELS,(char *)&i);
    
    // Set Sound Card Sampling Format to 16bit little-endian format
    // It must be checked, wheather S3C2410 support this format
    i=AFMT_S16_LE;
    ioctl (soundfd,SNDCTL_DSP_SETFMT,(char *)&i);
    
    // Enable precise timing in recording or playback
    i=3;
    ioctl (soundfd,SNDCTL_DSP_SETTRIGGER,(char *)&i);

    i=1;
    ioctl (soundfd,SNDCTL_DSP_PROFILE,(char *)&i);      

    for(;;)
    {
        // Try to send first, if circle queue is already full filled
        if(mQueue.IsFull())
        {
            write(sockfd, soundbuf, sizeof(soundbuf));
            continue;
        }
        else if(get_sample(soundfd, soundbuf))
        {
            if(get_sample(soundfd, soundbuf))
                mQueue.Insert((char *)soundbuf, (int &)frame_size);
        }
        
        // if circle queue has data, try to send a sample
        if(!mQueue.IsEmpty())
        {
            mQueue.Remove((char *)soundbuf, (int &)frame_size);
            write(sockfd, soundbuf, sizeof(soundbuf));
        }
    }

    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -