【问题标题】:ALSA tutorial required需要 ALSA 教程
【发布时间】:2012-01-19 02:38:48
【问题描述】:

我是音频编程的新手。我想创建能够播放并提供音量控制的小型应用程序。我正在使用 alsa-lib。

我想知道开关(例如主播放开关)的用途是什么,混音器元素中的枚举以及我应该为这些开关设置什么值。

请给我一些关于混音器设置和 alsa 编程的教程。

【问题讨论】:

    标签: c linux audio alsa


    【解决方案1】:

    只是在这里收集一些,有示例代码:

    请注意,其中一些是旧的,同时 API 可能已更改...您也可以查找 aplay.c(命令行 arecordaplay 的来源),但那个是对于初学者来说不是最容易阅读的......

    【讨论】:

      【解决方案2】:

      你很难在 ALSA 上找到任何具体的东西,正如我刚开始学习它时发现的那样。最好的起点是ALSA project homepage,它们链接到许多教程,最好的是 Nagorni 博士的 IMO。

      不过,从您的尝试看来,JACK 很可能是一个更快、更简单的解决方案。

      【讨论】:

        【解决方案3】:

        查看文档。有一些很好的例子。

        http://www.alsa-project.org/alsa-doc/alsa-lib/examples.html

        注意安全的 alsa 子集。

        https://www.winehq.org/pipermail/wine-bugs/2009-June/179698.html

        这是我使用我能找到的各种来源汇总的一些小东西。这可能是一个很好的起点。

        /* Compile with gcc -lasound -pthread threadaudio.c */
        
        #include <alsa/asoundlib.h>
        #include <pthread.h>
        #include <stdio.h>
        
        unsigned char audiobuffer[0x400];
        pthread_mutex_t audiomutex = PTHREAD_MUTEX_INITIALIZER;
        
        void changeaudio (int volume) {
          int i;
          pthread_mutex_lock(&audiomutex);
          for (i = 0; i < sizeof(audiobuffer); i++)
            audiobuffer[i] = (random() & 0xff) * volume / 10;
          pthread_mutex_unlock(&audiomutex);
        }
        
        void *startaudio (void *param)
        {
          static char *device = "default";
          snd_output_t *output = NULL;
          int *audiostop = (int*)param;
          int err;
          snd_pcm_t *handle;
          snd_pcm_sframes_t frames;
        
          changeaudio(5);
        
          if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
            printf("Playback open error: %s\n", snd_strerror(err));
            exit(EXIT_FAILURE);
          }
        
          if ((err = snd_pcm_set_params(handle,
                        SND_PCM_FORMAT_U8,
                        SND_PCM_ACCESS_RW_INTERLEAVED,
                        1,
                        48000,
                        1,
                        100000)) < 0) {   /* 0.1sec */
            printf("Playback open error: %s\n", snd_strerror(err));
            exit(EXIT_FAILURE);
          }
        
          while (!*audiostop) {
            err = snd_pcm_wait(handle, 1000);
            if (err < 0) {
              fprintf (stderr, "poll failed (%d)\n", err);
              break;
            }
        
            pthread_mutex_lock(&audiomutex);
            frames = snd_pcm_writei(handle, audiobuffer, sizeof(audiobuffer));
            pthread_mutex_unlock(&audiomutex);
        
            if (frames < 0)
              err = snd_pcm_recover(handle, frames, 0);
            if (err < 0) {
              printf("snd_pcm_writei failed: %s\n", snd_strerror(err));
              break;
            }
        
            if (frames > 0 && frames < (long)sizeof(audiobuffer))
              printf("Short write (expected %li, wrote %li)\n", (long)sizeof(audiobuffer), frames);
          }
          snd_pcm_close(handle);  
        }
        
        int main(void)
        {
          pthread_t audiothread;
          int audiostop = 0;
          int volume;
        
          pthread_create(&audiothread, NULL, startaudio, &audiostop);
        
          while (1) {
            printf("Enter volume 1 through 10. [0 to quit.]: ");
            scanf("%d", &volume);
            if (volume == 0) break;
            changeaudio(volume);
          }
        
          audiostop = 1;
          pthread_join(audiothread, NULL);
        
          return 0;
        }
        

        在阅读了上面的代码之后,您可能想阅读这篇关于(除其他外)不使用锁的文章。

        http://www.rossbencina.com/code/real-time-audio-programming-101-time-waits-for-nothing

        【讨论】:

          猜你喜欢
          • 2017-12-21
          • 1970-01-01
          • 1970-01-01
          • 2018-03-06
          • 2011-10-18
          • 2013-11-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多