title: Set mixer as "default" ALSA API
body: <p>I wrote a code to open and control mixer volume:</p>
  
  <pre><code>char *card, *channel;
  snd_mixer_t *handle = NULL;
  snd_mixer_elem_t *elem = NULL;
  
  static long alsa_min, alsa_max;
  
  void alsa_open_mixer( void )
  {
      int err;
      static snd_mixer_selem_id_t *sid = NULL;
      if ((err = snd_mixer_open (&amp;handle, 0)) &lt; 0)
          return;
      if ((err = snd_mixer_attach (handle, card)) &lt; 0)
          goto error;
      if ((err = snd_mixer_selem_register (handle, NULL, NULL)) &lt; 0)
          goto error;
      if ((err = snd_mixer_load (handle)) &lt; 0)
          goto error;
      snd_mixer_selem_id_malloc(&amp;sid);
      if (sid == NULL)
          goto error;
      snd_mixer_selem_id_set_name(sid, channel);
      if (!(elem = snd_mixer_find_selem(handle, sid)))
          goto error;
      if (!snd_mixer_selem_has_playback_volume(elem))
          goto error;
      snd_mixer_selem_get_playback_volume_range(elem, &amp;alsa_min, &amp;alsa_max);
      if ((alsa_max - alsa_min) &lt;= 0)
          goto error;
      snd_mixer_selem_id_free(sid);
      return;
  
  error:
      if (sid != NULL) {
          snd_mixer_selem_id_free(sid);
          sid = NULL;
      }
  
      if (handle) {
          snd_mixer_close(handle);
          handle = NULL;
      }
      return;
  }
  
  int alsa_set_device( const char *devname )
  {
      int i;
  
      if (card) free(card);
      card = strdup( devname );
      if( !card )
          return 0;
  
      i = strcspn( card, "/" );
      if( i == strlen( card ) ) {
          channel = "Line";
      } else {
          card[i] = 0;
          channel = card + i + 1;
      }
      alsa_open_mixer();
      if (!handle) {
          fprintf( stderr, "mixer: Can't open mixer '%s'.\n", card );
          return 0;
      }
      return 1;
  }
  </code></pre>
  
  <p>My questions is way can open mixer with:</p>
  
  <pre><code>alsa_set_device( "hw:0" )
  </code></pre>
  
  <p>and with:</p>
  
  <pre><code>alsa_set_device( "default" )
  </code></pre>
  
  <p>got error:</p>
  
  <pre><code>mixer: Can't open mixer 'default'.
  </code></pre>
  
  <p>Also I want to write a plugin to capture audio stream from TV card and output them in default mainboard sound card. I want to can control stream volume from my application using
  ALSA API. </p>
  
  <p>Maybe have porting to PulseAudio instead. In this case I need help with pulse mixer code equivalent with my above code.</p>
  
  <p>Thanks</p>