2015-11-18 6 views
0

Я новичок в PulseAudio. Я пытаюсь сделать простые программы. Можно записать звук и сохранить его в банарном файле, а другой - открыть его и сыграть. Вот мой код для записи:PulseAudio: звук записан, но играет раздражающий звук

#ifdef HAVE_CONFIG_H 
    #include <config.h> 
#endif 

#include <stdio.h> 
#include <unistd.h> 
#include <string.h> 
#include <errno.h> 
#include <fcntl.h> 
#include <stdlib.h> 
#include <pulse/simple.h> 
#include <pulse/error.h> 

#define BUFSIZE 32 

int main(int argc, char*argv[]) 
{ 

    /* The Sample format to use */ 
    static const pa_sample_spec ss = { 
       .format = PA_SAMPLE_S16LE, //16bit iqneba agwerili tito sample 
       .rate = 44100, //number of samples played in each second 
       .channels = 2 
      }; 

    pa_simple *s_in = NULL; 
    int ret = 1; 
    int error; 
    int siri =0; 


    //file info 
    FILE* pFile; 
    char* yourFilePath = "xma.bin"; 
    pFile = fopen(yourFilePath,"wb"); 





    if (!(s_in = pa_simple_new(NULL, argv[0], PA_STREAM_RECORD, NULL, "record", &ss, NULL, NULL, &error))) 
    { 
     fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error)); 
     goto finish; 
    } 

    for (;siri<10000;siri+=1) 
    { 
     uint8_t buf[BUFSIZE]; 
     ssize_t r; 

     int yorBufferSize = strlen(buf) + 1; 


     /* Write your buffer to disk. */ 


     if (pa_simple_read(s_in, buf, sizeof(buf), &error) < 0) 
     { 

      fprintf(stderr, __FILE__": read() failed: %s\n", strerror(errno)); 
      goto finish; 
     } 



     if (pFile) 
     { 
      fwrite(buf, yorBufferSize, 1, pFile); 
      puts("Wrote to file!"); 
     } 
     else 
     { 
      puts("Something wrong writing to File."); 
     } 


    } 


    ret = 0; 

    finish: 

    if (s_in) 
     pa_simple_free(s_in); 


    return ret; 
    fclose(pFile); 
} 

А вот моя программа записи:

#ifdef HAVE_CONFIG_H 
#include <config.h> 
#endif 

#include <stdio.h> 
#include <unistd.h> 
#include <string.h> 
#include <errno.h> 
#include <fcntl.h> 
#include <stdlib.h> 
#include <pulse/simple.h> 
#include <pulse/error.h> 

#define BUFSIZE 32 

int main(int argc, char*argv[]) 
{ 

    /* The Sample format to use */ 
    static const pa_sample_spec ss = { 
     .format = PA_SAMPLE_S16LE, //16bit iqneba agwerili tito sample 
     .rate = 44100, //number of samples played in each second 
     .channels = 2 
    }; 

    pa_simple *s_out = NULL; 
    int ret = 1; 
    int error; 


    //file info 
    FILE* pFile; 
    char* yourFilePath = "xma.bin"; 
    pFile = fopen(yourFilePath, "rb"); 



    /* Create a new playback stream */ 
    if (!(s_out = pa_simple_new(NULL, argv[0], PA_STREAM_PLAYBACK, NULL, "playback", &ss, NULL, NULL, &error))) 
    { 
     fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error)); 
     goto finish; 
    } 

    for (;;) 
    { 
     uint8_t buf[BUFSIZE]; 
     fread(buf, sizeof(buf), 1, pFile); 
     ssize_t r; 

     if(feof(pFile)) 
     { 
      break; 
     } 


     printf("%x", buf); 

     /* ... and play it */ 
     if (pa_simple_write(s_out, buf, sizeof(buf), &error) < 0) 
     { 
      fprintf(stderr, __FILE__": pa_simple_write() failed: %s\n", pa_strerror(error)); 
      goto finish; 
     } 


    } 


    /* Make sure that every single sample was played */ 
    if (pa_simple_drain(s_out, &error) < 0) 
    { 
     fprintf(stderr, __FILE__": pa_simple_drain() failed: %s\n", pa_strerror(error)); 
     goto finish; 
    } 

    ret = 0; 

    finish: 

    if (s_out) 
     pa_simple_free(s_out); 

    return ret; 
    fclose(pFile); 
} 

Для цикла в записи программы только на время, чтобы записать что-то (не мог понять, как установить таймер), и я знаю, что я не должен использовать gotos, но его для образовательных целей (пример представлен на веб-сайте PulseAudio). Я попробовал hexdump xma.bin, и он дал мне совершенно другой вывод , чем printf ("% x", buf); В основном printf возвращает только bf9fe15c и вызывает раздражающий звук. Надеюсь, ты поможешь. Благодарю.

+0

Не могли бы вы сконденсировать свой примерный код и удалить все, что не является необходимым для появления проблемы? Это упростит ответ. – Trilarion

+0

https://stackoverflow.com/help/mcve – 4ae1e1

ответ

0

Я удалил pa_simple_drain() (это была моя ошибка, что я использовал эту функцию в программе записи) из программы записи, и теперь она работает. Но в printf («% x», buf) он все еще возвращает мне одно и то же значение шестнадцатеричного значения снова и снова. Но программы отлично работают. Может ли кто-нибудь понять, почему он печатает одинаковое значение?

+0

Проверить предупреждения: 60: 9: предупреждение: формат «% x» ожидает аргумент типа «unsigned int», но аргумент 2 имеет тип «uint8_t *» [-Wformat = ] printf ("% x", buf); ^ – Michi

+0

Я изменил его на printf ("% x", (unsigned int) buf); но не работает – Ojs