代码之家  ›  专栏  ›  技术社区  ›  Michael

用c代码在ubuntu中向声卡发送原始数据

  •  0
  • Michael  · 技术社区  · 7 年前

    我试图用ubuntu中的c代码将从串行端口外部获得的原始数据写入声卡设备,以便播放它。

    我到处寻找解决方案,发现它可以通过ALSA API完成,但无法提供任何关于我应该如何做的明确例子。 我也试过使用ioctl()函数-但它不能播放。

    #include <stdio.h>
    #include <fcntl.h>  // File Control Definitions 
    #include <string.h>
    #include <pthread.h>
    #include <semaphore.h> 
    #include <stdlib.h>
    #include <termios.h>
    #include <unistd.h>
    #include <sys/ioctl.h>
    #include <linux/kd.h>
    int flg = 0, rdlen = 0;
    int com_ptr, sound_ptr;
    char input_data[16];
    char output_data[16];
    sem_t data_mutex; 
    
    
    void* read_data(void *arg)
    {
        int i, j;
    
        for (j = 0 ; j < 5 ; j++)
        {
            while(flg == 1);//waits for the data to be read
            sem_wait(&data_mutex);
            printf("\ndata_read_mutex MUTEX TAKEN\n");
            printf("read\n");
    
            rdlen = read(com_ptr, input_data, sizeof(input_data));
            //fwrite(buf, 1, rdlen, log_file);
            printf("input length : %d\n",rdlen);
    
            for(i = 0 ; i < rdlen - 1; i++)
            {
                //input_data[i] = data[i];
                printf("Reading.... : %c\n", input_data[i]);
            }//for
    
            if(sem_post(&data_mutex))
            {
                printf("ERROR POSTING data_mutex\n");
            }//if
            else
            {
                flg = 1;
                printf("data_mutex WAS POSTED\n");
            }
        }//for
    }//read_data
    
    void* play_data(void *arg)
    {
        int i, j;
    
        for (j = 0 ; j < 5 ; j++)
        {
            while (flg == 0);// waits for the data to be redy
            sem_wait(&data_mutex);
            printf("\ndata_ready_mutex MUTEX TAKEN\n");
            printf("play\n");
    
            for(i = 0 ; i < rdlen - 1; i++)
            {
                output_data[i] = input_data[i];
                printf("Playing.... : %c\n", output_data[i]);
                //rdlen = write(sound_ptr, input_data, sizeof(input_data));
                //ioctl(sound_ptr, KIOCSOUND, 119);
                //usleep(500000);
                //sound(output_data[i] - 48);
            }//for
    
            if(sem_post(&data_mutex))
            {
                printf("ERROR POSTING data_mutex\n");
            }//if
            else
            {
                flg = 0;
                printf("data_mutex WAS POSTED\n");
            }
        }//for
    }//play_data
    
    int main(int argc, char **argv)
    {
        int i;
    
        sem_init(&data_mutex, 0, 1); 
    
        void * read_return, *play_return;
        pthread_t play_t, read_t;
    
    
    
        if (argc == 3)
        {
            printf( "input address = %s\n", argv[1]);
            printf( "output address = %s\n", argv[2]);
        }
    
    
        com_ptr = open("/dev/stdin", O_RDWR | O_NOCTTY);
        sound_ptr = open("/proc/asound/card0", 'w');
        if(com_ptr == -1)
            printf("\nError! in Opening ttyUSB0  ");
        else
            printf("\nttyUSB0 Opened Successfully ");
    
    
        if (pthread_create(&read_t, NULL, &read_data, NULL))
        {
            perror("read pthread_create() error");
            exit(1);
        }//if
    
        sleep(1); // ensuring the read gets the first round
    
        if (pthread_create(&play_t, NULL, &play_data, NULL))
        {
            perror("play pthread_create() error");
            exit(1);
        }//if
    
        if (pthread_join(read_t, &read_return))
        {
            perror("read pthread_join() error");
            exit(1);
        }//if
        if (pthread_join(play_t, &play_return))
        {
            perror("play pthread_join() error");
            exit(1);
        }//if
    
        sem_destroy(&data_mutex); 
    
        return 0;
    }//main
    

    我正在获取数据并可以读取它-但我需要将它写入声卡-这是我做不到的。

    0 回复  |  直到 7 年前