Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
media-sound/moc
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Multimedia
View previous topic :: View next topic  
Author Message
antonellocaroli
Guru
Guru


Joined: 11 Aug 2016
Posts: 509

PostPosted: Sun Sep 05, 2021 10:25 am    Post subject: media-sound/moc Reply with quote

I can't get moc 2.6_alpha3-r5 to work at all

no matter how I set alsadevice I always get the same error:

Code:
Running the server...
Trying ALSA...

FATAL_ERROR: No valid sound driver!


FATAL_ERROR: Server exited!


the only way to make it work is to replace alsa.c in the sources with the one from version 2.5 and recompile it

I don't understand C language, so I don't know what the problem is....

I don't even know if it's a problem that only occurs on my system....

can someone who uses moc verify that they don't have the same problem as me?
Back to top
View user's profile Send private message
WesleyL
n00b
n00b


Joined: 18 Oct 2021
Posts: 7
Location: São Paulo, Brazil

PostPosted: Mon Oct 18, 2021 10:06 pm    Post subject: Reply with quote

It has always worked just fine for me, not sure why you're facing this issue. Are you sure you compiled it with the alsa USE flag enabled?
Back to top
View user's profile Send private message
mike155
Advocate
Advocate


Joined: 17 Sep 2010
Posts: 4438
Location: Frankfurt, Germany

PostPosted: Tue Oct 19, 2021 1:54 am    Post subject: Reply with quote

Pequeno Sapo,

welcome to the Gentoo forums!

You're right! I just installed moc 2.6_alpha3-r5. It works perfectly fine for me. I get the error message shown in the first post if I emerge moc WITHOUT USE flag "alsa".

@antonellocaroli: please check your USE flags! :)

Mike
Back to top
View user's profile Send private message
antonellocaroli
Guru
Guru


Joined: 11 Aug 2016
Posts: 509

PostPosted: Tue Oct 19, 2021 3:52 am    Post subject: Reply with quote

clearly the USE flags alsa are active...

Code:
emerge -pv =media-sound/moc-2.6_alpha3-r5

These are the packages that would be merged, in order:

Calculating dependencies... done!
[ebuild   R    ] media-sound/moc-2.6_alpha3-r5::gentoo  USE="aac alsa cache curl ffmpeg flac libsamplerate mad magic unicode -debug -jack -modplug -musepack -oss -sid -sndfile -sndio -speex -timidity -tremor -vorbis -wavpack" 0 KiB


but i forgot to say that it works with the pc's internal sound card, instead i get the above error with an external usb dac.

I solve this problem either by replacing alsa.c in the sources, as I wrote above, or by installing version 2.5.2-r3.
Back to top
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 21607

PostPosted: Tue Oct 19, 2021 2:56 pm    Post subject: Reply with quote

If reverting to an older version of one file fixes it, that suggests there is a change in that file that causes a regression for the OP. OP: what differences exist between the two versions of the file? If you bisect intermediate commits between the good and bad versions, can you find the first commit where its alsa.c is bad for you?
Back to top
View user's profile Send private message
antonellocaroli
Guru
Guru


Joined: 11 Aug 2016
Posts: 509

PostPosted: Tue Oct 19, 2021 3:53 pm    Post subject: Reply with quote

the diff is this:

https://github.com/antonellocaroli/mocp1/commit/ed73fb31b251abc97ac48702a6fd7b827e2403e7

but I don't understand the C language so I can't understand what the problem might be...
Back to top
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 21607

PostPosted: Tue Oct 19, 2021 4:05 pm    Post subject: Reply with quote

I read C, but that's a rather large diff. I don't have the time today to dig through a diff of such a large unfamiliar file, in any language. Can you bisect it their code to find the specific commit in which this breaks?
Back to top
View user's profile Send private message
antonellocaroli
Guru
Guru


Joined: 11 Aug 2016
Posts: 509

PostPosted: Wed Jan 19, 2022 3:53 am    Post subject: Reply with quote

the problem seems to arise after this patch

Code:
@@ -48,6 +48,7 @@ static int chunk_bytes = -1;
 static char alsa_buf[512 * 1024];
 static int alsa_buf_fill = 0;
 static int bytes_per_frame;
+static int bytes_per_sample;
 
 static snd_mixer_t *mixer_handle = NULL;
 static snd_mixer_elem_t *mixer_elem1 = NULL;
@@ -125,45 +126,42 @@ static void alsa_log_cb (const char *unu
 }
 #endif
 
-static void alsa_shutdown ()
-{
-   int err;
-
-   if (mixer_handle && (err = snd_mixer_close(mixer_handle)) < 0)
-      logit ("Can't close mixer: %s", snd_strerror(err));
-
-#ifndef NDEBUG
-   snd_lib_error_set_handler (NULL);
-#endif
-}
-
 static snd_pcm_hw_params_t *alsa_open_device (const char *device)
 {
-   snd_pcm_hw_params_t *result;
    int rc;
+   snd_pcm_hw_params_t *result;
+
+   assert (!handle);
 
    rc = snd_pcm_open (&handle, device, SND_PCM_STREAM_PLAYBACK,
                                        SND_PCM_NONBLOCK);
    if (rc < 0) {
       error ("Can't open audio: %s", snd_strerror (rc));
-      return NULL;
+      goto err1;
    }
 
    rc = snd_pcm_hw_params_malloc (&result);
    if (rc < 0) {
-      error ("Can't allocate alsa hardware parameters structure: %s",
+      error ("Can't allocate ALSA hardware parameters structure: %s",
               snd_strerror (rc));
-      snd_pcm_close (handle);
-      return NULL;
+      goto err2;
    }
 
    rc = snd_pcm_hw_params_any (handle, result);
    if (rc < 0) {
       error ("Can't initialize hardware parameters structure: %s",
               snd_strerror (rc));
+      goto err3;
+   }
+
+   if (0) {
+   err3:
       snd_pcm_hw_params_free (result);
+   err2:
       snd_pcm_close (handle);
-      return NULL;
+   err1:
+      result = NULL;
+      handle = NULL;
    }
 
    return result;
@@ -172,81 +170,97 @@ static snd_pcm_hw_params_t *alsa_open_de
 /* Fill caps with the device capabilities. Return 0 on error. */
 static int fill_capabilities (struct output_driver_caps *caps)
 {
+   int result = 0;
    snd_pcm_hw_params_t *hw_params;
-   snd_pcm_format_mask_t *format_mask;
-   int err;
-   unsigned int val;
+
+   assert (!handle);
 
    hw_params = alsa_open_device (options_get_str ("ALSADevice"));
    if (!hw_params)
       return 0;
 
-   if ((err = snd_pcm_hw_params_get_channels_min (hw_params, &val)) < 0) {
-      error ("Can't get the minimum number of channels: %s",
-            snd_strerror(err));
-      snd_pcm_hw_params_free (hw_params);
-      snd_pcm_close (handle);
-      return 0;
-   }
-   caps->min_channels = val;
+   do {
+      int rc;
+      unsigned int val;
+      snd_pcm_format_mask_t *format_mask;
 
-   if ((err = snd_pcm_hw_params_get_channels_max (hw_params, &val)) < 0) {
-      error ("Can't get the maximum number of channels: %s",
-            snd_strerror(err));
-      snd_pcm_hw_params_free (hw_params);
-      snd_pcm_close (handle);
-      return 0;
-   }
-   caps->max_channels = val;
+      rc = snd_pcm_hw_params_get_channels_min (hw_params, &val);
+      if (rc < 0) {
+         error ("Can't get the minimum number of channels: %s",
+                 snd_strerror (rc));
+         break;
+      }
+      caps->min_channels = val;
 
-   if ((err = snd_pcm_format_mask_malloc(&format_mask)) < 0) {
-      error ("Can't allocate format mask: %s", snd_strerror(err));
-      snd_pcm_hw_params_free (hw_params);
-      snd_pcm_close (handle);
-      return 0;
-   }
-   snd_pcm_hw_params_get_format_mask (hw_params, format_mask);
-   caps->formats = mask_to_format (format_mask) | SFMT_NE;
+      rc = snd_pcm_hw_params_get_channels_max (hw_params, &val);
+      if (rc < 0) {
+         error ("Can't get the maximum number of channels: %s",
+                 snd_strerror (rc));
+         break;
+      }
+      caps->max_channels = val;
+
+      rc = snd_pcm_format_mask_malloc (&format_mask);
+      if (rc < 0) {
+         error ("Can't allocate format mask: %s", snd_strerror (rc));
+         break;
+      }
+      snd_pcm_hw_params_get_format_mask (hw_params, format_mask);
+      caps->formats = mask_to_format (format_mask) | SFMT_NE;
+      snd_pcm_format_mask_free (format_mask);
+
+      result = 1;
+   } while (0);
 
-   snd_pcm_format_mask_free (format_mask);
    snd_pcm_hw_params_free (hw_params);
    snd_pcm_close (handle);
    handle = NULL;
 
-   return 1;
+   return result;
 }
 
 static void handle_mixer_events (snd_mixer_t *mixer_handle)
 {
-   int count;
+   struct pollfd *fds = NULL;
 
-   if ((count = snd_mixer_poll_descriptors_count(mixer_handle)) < 0)
-      logit ("snd_mixer_poll_descriptors_count() failed: %s",
-            snd_strerror(count));
-   else {
-      struct pollfd *fds;
-      int rc;
+   assert (mixer_handle);
 
-      fds = xcalloc (count, sizeof(struct pollfd));
+   do {
+      int rc, count;
 
-      if ((rc = snd_mixer_poll_descriptors(mixer_handle, fds,
-                  count)) < 0)
+      count = snd_mixer_poll_descriptors_count (mixer_handle);
+      if (count < 0) {
+         logit ("snd_mixer_poll_descriptors_count() failed: %s",
+                 snd_strerror (count));
+         break;
+      }
+
+      fds = xcalloc (count, sizeof (struct pollfd));
+
+      rc = snd_mixer_poll_descriptors (mixer_handle, fds, count);
+      if (rc < 0) {
          logit ("snd_mixer_poll_descriptors() failed: %s",
-               snd_strerror(rc));
-      else {
-         rc = poll (fds, count, 0);
-         if (rc < 0)
-            error_errno ("poll() failed", errno);
-         else if (rc > 0) {
-            debug ("Mixer event");
-            if ((rc = snd_mixer_handle_events(mixer_handle)) < 0)
-               logit ("snd_mixer_handle_events() failed: %s",
-                     snd_strerror(rc));
-         }
+                 snd_strerror (rc));
+         break;
       }
 
-      free (fds);
-   }
+      rc = poll (fds, count, 0);
+      if (rc < 0) {
+         error_errno ("poll() failed", errno);
+         break;
+      }
+
+      if (rc == 0)
+         break;
+
+      debug ("Mixer event");
+
+      rc = snd_mixer_handle_events (mixer_handle);
+      if (rc < 0)
+         logit ("snd_mixer_handle_events() failed: %s", snd_strerror (rc));
+   } while (0);
+
+   free (fds);
 }
 
 static int alsa_read_mixer_raw (snd_mixer_elem_t *elem)
@@ -308,35 +322,130 @@ static int alsa_read_mixer_raw (snd_mixe
 static snd_mixer_elem_t *alsa_init_mixer_channel (const char *name)
 {
    snd_mixer_selem_id_t *sid;
-   snd_mixer_elem_t *elem = NULL;
+   snd_mixer_elem_t *result = NULL;
+
+   assert (mixer_handle);
 
    snd_mixer_selem_id_malloc (&sid);
    snd_mixer_selem_id_set_index (sid, 0);
    snd_mixer_selem_id_set_name (sid, name);
 
-   if (!(elem = snd_mixer_find_selem(mixer_handle, sid)))
-      error ("Can't find mixer %s", name);
-   else if (!snd_mixer_selem_has_playback_volume(elem)) {
-      error ("Mixer device has no playback volume (%s).", name);
-      elem = NULL;
-   }
-   else if (snd_mixer_selem_set_playback_volume_range (elem, 0, 100) < 0) {
-      error ("Cannot set playback volume range (%s).", name);
-      elem = NULL;
-   }
-   else
+   do {
+      snd_mixer_elem_t *elem = NULL;
+
+      elem = snd_mixer_find_selem (mixer_handle, sid);
+      if (!elem) {
+         error ("Can't find mixer %s", name);
+         break;
+      }
+
+      if (!snd_mixer_selem_has_playback_volume (elem)) {
+         error ("Mixer device has no playback volume (%s).", name);
+         break;
+      }
+
+      if (snd_mixer_selem_set_playback_volume_range (elem, 0, 100) < 0) {
+         error ("Cannot set playback volume range (%s).", name);
+         break;
+      }
+
       logit ("Opened mixer (%s)", name);
+      result = elem;
+   } while (0);
 
    snd_mixer_selem_id_free (sid);
 
-   return elem;
+   return result;
+}
+
+static void alsa_close_mixer ()
+{
+   if (mixer_handle) {
+      int rc;
+
+      rc = snd_mixer_close (mixer_handle);
+      if (rc < 0)
+         logit ("Can't close mixer: %s", snd_strerror (rc));
+
+      mixer_handle = NULL;
+   }
+}
+
+static void alsa_open_mixer (const char *device)
+{
+   int rc;
+
+   assert (!mixer_handle);
+
+   rc = snd_mixer_open (&mixer_handle, 0);
+   if (rc < 0) {
+      error ("Can't open ALSA mixer: %s", snd_strerror (rc));
+      goto err;
+   }
+
+   rc = snd_mixer_attach (mixer_handle, device);
+   if (rc < 0) {
+      error ("Can't attach mixer: %s", snd_strerror (rc));
+      goto err;
+   }
+
+   rc = snd_mixer_selem_register (mixer_handle, NULL, NULL);
+   if (rc < 0) {
+      error ("Can't register mixer: %s", snd_strerror (rc));
+      goto err;
+   }
+
+   rc = snd_mixer_load (mixer_handle);
+   if (rc < 0) {
+      error ("Can't load mixer: %s", snd_strerror (rc));
+      goto err;
+   }
+
+   if (0) {
+   err:
+      alsa_close_mixer ();
+   }
+}
+
+static void alsa_set_current_mixer ()
+{
+   int vol;
+
+   if (mixer_elem1 && (vol = alsa_read_mixer_raw (mixer_elem1)) != -1) {
+      assert (RANGE(0, vol, 100));
+      volume1 = vol;
+   }
+   else {
+      mixer_elem1 = NULL;
+      mixer_elem_curr = mixer_elem2;
+   }
+
+   if (mixer_elem2 && (vol = alsa_read_mixer_raw (mixer_elem2)) != -1) {
+      assert (RANGE(0, vol, 100));
+      volume2 = vol;
+   }
+   else {
+      mixer_elem2 = NULL;
+      mixer_elem_curr = mixer_elem1;
+   }
+}
+
+static void alsa_shutdown ()
+{
+   alsa_close_mixer ();
+
+#ifndef NDEBUG
+   snd_lib_error_set_handler (NULL);
+#endif
 }
 
 static int alsa_init (struct output_driver_caps *caps)
 {
-   int err;
+   int result = 0;
    const char *device;
 
+   assert (!mixer_handle);
+
    device = options_get_str ("ALSADevice");
    logit ("Initialising ALSA device: %s", device);
 
@@ -344,26 +453,7 @@ static int alsa_init (struct output_driv
    snd_lib_error_set_handler (alsa_log_cb);
 #endif
 
-   if ((err = snd_mixer_open(&mixer_handle, 0)) < 0) {
-      error ("Can't open ALSA mixer: %s", snd_strerror(err));
-      mixer_handle = NULL;
-   }
-   else if ((err = snd_mixer_attach(mixer_handle, device)) < 0) {
-      snd_mixer_close (mixer_handle);
-      mixer_handle = NULL;
-      error ("Can't attach mixer: %s", snd_strerror(err));
-   }
-   else if ((err = snd_mixer_selem_register(mixer_handle, NULL, NULL))
-         < 0) {
-      snd_mixer_close (mixer_handle);
-      mixer_handle = NULL;
-      error ("Can't register mixer: %s", snd_strerror(err));
-   }
-   else if ((err = snd_mixer_load(mixer_handle)) < 0) {
-      snd_mixer_close (mixer_handle);
-      mixer_handle = NULL;
-      error ("Can't load mixer: %s", snd_strerror(err));
-   }
+   alsa_open_mixer (device);
 
    if (mixer_handle) {
       mixer_elem1 = alsa_init_mixer_channel (options_get_str ("ALSAMixer1"));
@@ -372,48 +462,33 @@ static int alsa_init (struct output_driv
 
    mixer_elem_curr = mixer_elem1 ? mixer_elem1 : mixer_elem2;
 
-   if (mixer_elem_curr) {
-      int vol;
-
-      if (mixer_elem1 && (vol = alsa_read_mixer_raw (mixer_elem1)) != -1) {
-         assert (RANGE(0, vol, 100));
-         volume1 = vol;
-      }
-      else {
-         mixer_elem1 = NULL;
-         mixer_elem_curr = mixer_elem2;
-      }
+   if (mixer_elem_curr)
+      alsa_set_current_mixer ();
 
-      if (mixer_elem2 && (vol = alsa_read_mixer_raw (mixer_elem2)) != -1) {
-         assert (RANGE(0, vol, 100));
-         volume2 = vol;
-      }
-      else {
-         mixer_elem2 = NULL;
-         mixer_elem_curr = mixer_elem1;
-      }
+   if (!mixer_elem_curr)
+      goto err;
 
-      if (!mixer_elem_curr) {
-         snd_mixer_close (mixer_handle);
-         mixer_handle = NULL;
-      }
-   }
-   else if (mixer_handle) {
-      snd_mixer_close (mixer_handle);
-      mixer_handle = NULL;
+   result = fill_capabilities (caps);
+   if (result == 0)
+      goto err;
+
+   if (0) {
+   err:
+      alsa_shutdown ();
    }
 
-   return fill_capabilities (caps);
+   return result;
 }
 
 static int alsa_open (struct sound_params *sound_params)
 {
-   snd_pcm_hw_params_t *hw_params;
-   int err;
-   unsigned int period_time;
-   unsigned int buffer_time;
+   int rc, result = 0;
+   unsigned int period_time, buffer_time;
    char fmt_name[128];
    const char *device;
+   snd_pcm_hw_params_t *hw_params;
+
+   assert (!handle);
 
    params.format = format_to_mask (sound_params->fmt & SFMT_MASK_FORMAT);
    if (params.format == SND_PCM_FORMAT_UNKNOWN) {
@@ -429,69 +504,67 @@ static int alsa_open (struct sound_param
    if (!hw_params)
       return 0;
 
-   if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
-      error ("Can't set alsa access type: %s", snd_strerror(err));
-      snd_pcm_hw_params_free (hw_params);
-      return 0;
+   rc = snd_pcm_hw_params_set_access (handle, hw_params,
+                                      SND_PCM_ACCESS_RW_INTERLEAVED);
+   if (rc < 0) {
+      error ("Can't set ALSA access type: %s", snd_strerror (rc));
+      goto err;
    }
 
-   if ((err = snd_pcm_hw_params_set_format (handle, hw_params,
-               params.format)) < 0) {
-      error ("Can't set sample format: %s", snd_strerror(err));
-      snd_pcm_hw_params_free (hw_params);
-      return 0;
+   rc = snd_pcm_hw_params_set_format (handle, hw_params, params.format);
+   if (rc < 0) {
+      error ("Can't set sample format: %s", snd_strerror (rc));
+      goto err;
    }
 
-   logit ("Set sample width: %d bytes", sfmt_Bps (sound_params->fmt));
+   bytes_per_sample = sfmt_Bps (sound_params->fmt);
+   logit ("Set sample width: %d bytes", bytes_per_sample);
 
    params.rate = sound_params->rate;
-   if ((err = snd_pcm_hw_params_set_rate_near (handle, hw_params,
-               &params.rate, 0)) < 0) {
-      error ("Can't set sample rate: %s", snd_strerror(err));
-      snd_pcm_hw_params_free (hw_params);
-      return 0;
+   rc = snd_pcm_hw_params_set_rate_near (handle, hw_params, &params.rate, 0);
+   if (rc < 0) {
+      error ("Can't set sample rate: %s", snd_strerror (rc));
+      goto err;
    }
 
    logit ("Set rate: %uHz", params.rate);
 
-   if ((err = snd_pcm_hw_params_set_channels (handle, hw_params,
-               sound_params->channels)) < 0) {
-      error ("Can't set number of channels: %s", snd_strerror(err));
-      snd_pcm_hw_params_free (hw_params);
-      return 0;
+   rc = snd_pcm_hw_params_set_channels (handle, hw_params,
+                                        sound_params->channels);
+   if (rc < 0) {
+      error ("Can't set number of channels: %s", snd_strerror (rc));
+      goto err;
    }
 
    logit ("Set channels: %d", sound_params->channels);
 
-   if ((err = snd_pcm_hw_params_get_buffer_time_max(hw_params,
-               &buffer_time, 0)) < 0) {
-      error ("Can't get maximum buffer time: %s", snd_strerror(err));
-      snd_pcm_hw_params_free (hw_params);
-      return 0;
+   rc = snd_pcm_hw_params_get_buffer_time_max (hw_params, &buffer_time, 0);
+   if (rc < 0) {
+      error ("Can't get maximum buffer time: %s", snd_strerror (rc));
+      goto err;
    }
 
-   if (buffer_time > BUFFER_MAX_USEC)
-      buffer_time = BUFFER_MAX_USEC;
+   buffer_time = MIN(buffer_time, BUFFER_MAX_USEC);
    period_time = buffer_time / 4;
 
-   if ((err = snd_pcm_hw_params_set_period_time_near(handle, hw_params,
-               &period_time, 0)) < 0) {
-      error ("Can't set period time: %s", snd_strerror(err));
-      snd_pcm_hw_params_free (hw_params);
-      return 0;
+   rc = snd_pcm_hw_params_set_period_time_near (handle, hw_params,
+                                                &period_time, 0);
+   if (rc < 0) {
+      error ("Can't set period time: %s", snd_strerror (rc));
+      goto err;
    }
 
-   if ((err = snd_pcm_hw_params_set_buffer_time_near(handle, hw_params,
-               &buffer_time, 0)) < 0) {
-      error ("Can't set buffer time: %s", snd_strerror(err));
-      snd_pcm_hw_params_free (hw_params);
-      return 0;
+   rc = snd_pcm_hw_params_set_buffer_time_near (handle, hw_params,
+                                                &buffer_time, 0);
+   if (rc < 0) {
+      error ("Can't set buffer time: %s", snd_strerror (rc));
+      goto err;
    }
 
-   if ((err = snd_pcm_hw_params (handle, hw_params)) < 0) {
-      error ("Can't set audio parameters: %s", snd_strerror(err));
-      snd_pcm_hw_params_free (hw_params);
-      return 0;
+   rc = snd_pcm_hw_params (handle, hw_params);
+   if (rc < 0) {
+      error ("Can't set audio parameters: %s", snd_strerror (rc));
+      goto err;
    }
 
    snd_pcm_hw_params_get_period_size (hw_params, &chunk_frames, 0);
@@ -502,7 +575,7 @@ static int alsa_open (struct sound_param
    debug ("Buffer time: %"PRIu64"us",
            (uint64_t) buffer_frames * UINT64_C(1000000) / params.rate);
 
-   bytes_per_frame = sound_params->channels * sfmt_Bps(sound_params->fmt);
+   bytes_per_frame = sound_params->channels * bytes_per_sample;
    debug ("Frame size: %d bytes", bytes_per_frame);
 
    chunk_bytes = chunk_frames * bytes_per_frame;
@@ -510,23 +583,26 @@ static int alsa_open (struct sound_param
    if (chunk_frames == buffer_frames) {
       error ("Can't use period equal to buffer size (%lu == %lu)",
             chunk_frames, buffer_frames);
-      snd_pcm_hw_params_free (hw_params);
-      return 0;
+      goto err;
    }
 
-   snd_pcm_hw_params_free (hw_params);
-
-   if ((err = snd_pcm_prepare(handle)) < 0) {
+   rc = snd_pcm_prepare (handle);
+   if (rc < 0) {
       error ("Can't prepare audio interface for use: %s",
-            snd_strerror(err));
-      return 0;
+              snd_strerror (rc));
+      goto err;
    }
 
    logit ("ALSA device opened");
 
    params.channels = sound_params->channels;
    alsa_buf_fill = 0;
-   return 1;
+   result = 1;
+
+err:
+   snd_pcm_hw_params_free (hw_params);
+
+   return result;
 }
 
 /* Play from alsa_buf as many chunks as possible. Move the remaining data
@@ -540,8 +616,7 @@ static int play_buf_chunks ()
    while (alsa_buf_fill >= chunk_bytes) {
       int rc;
 
-      rc = snd_pcm_writei (handle, alsa_buf + written,
-            chunk_bytes / bytes_per_frame);
+      rc = snd_pcm_writei (handle, alsa_buf + written, chunk_frames);
 
       if (rc == 0) {
          if (!zero_logged) {
@@ -591,13 +666,15 @@ static void alsa_close ()
    assert (handle != NULL);
 
    /* play what remained in the buffer */
-   if (alsa_buf_fill) {
+   if (alsa_buf_fill > 0) {
+      unsigned int samples_required;
+
       assert (alsa_buf_fill < chunk_bytes);
 
-      snd_pcm_format_set_silence (params.format,
-            alsa_buf + alsa_buf_fill,
-            (chunk_bytes - alsa_buf_fill) / bytes_per_frame
-            * params.channels);
+      samples_required = (chunk_bytes - alsa_buf_fill) / bytes_per_sample;
+      snd_pcm_format_set_silence (params.format, alsa_buf + alsa_buf_fill,
+                           samples_required);
+
       alsa_buf_fill = chunk_bytes;
       play_buf_chunks ();
    }
@@ -674,60 +751,78 @@ static int alsa_read_mixer ()
 
 static void alsa_set_mixer (int vol)
 {
+   int rc;
+
    assert (RANGE(0, vol, 100));
 
-   if (mixer_handle) {
-      int rc;
+   if (!mixer_handle)
+      return;
 
-      if (mixer_elem_curr == mixer_elem1)
-         volume1 = vol;
-      else
-         volume2 = vol;
+   if (mixer_elem_curr == mixer_elem1)
+      volume1 = vol;
+   else
+      volume2 = vol;
 
-      debug ("Setting vol to %d", vol);
+   debug ("Setting vol to %d", vol);
 
-      rc = snd_mixer_selem_set_playback_volume_all (mixer_elem_curr, vol);
-      if (rc < 0)
-         error ("Can't set mixer: %s", snd_strerror (rc));
-   }
+   rc = snd_mixer_selem_set_playback_volume_all (mixer_elem_curr, vol);
+   if (rc < 0)
+      error ("Can't set mixer: %s", snd_strerror (rc));
 }
 
 static int alsa_get_buff_fill ()
 {
-   if (handle) {
-      int err;
+   int result = 0;
+
+   do {
+      int rc;
       snd_pcm_sframes_t delay;
 
-      if ((err = snd_pcm_delay(handle, &delay)) < 0) {
-         logit ("snd_pcm_delay() failed: %s", snd_strerror(err));
-         return 0;
+      if (!handle)
+         break;
+
+      rc = snd_pcm_delay (handle, &delay);
+      if (rc < 0) {
+         logit ("snd_pcm_delay() failed: %s", snd_strerror (rc));
+         break;
       }
 
-      /* delay can be negative when underrun occur */
-      return MAX(delay, 0) * bytes_per_frame;
-   }
-   return 0;
+      /* delay can be negative if an underrun occurs */
+      result = MAX(delay, 0) * bytes_per_frame;
+   } while (0);
+
+   return result;
 }
 
 static int alsa_reset ()
 {
-   if (handle) {
-      int err;
+   int result = 0;
+
+   do {
+      int rc;
 
-      if ((err = snd_pcm_drop(handle)) < 0) {
-         error ("Can't reset the device: %s", snd_strerror(err));
-         return 0;
-      }
-      if ((err = snd_pcm_prepare(handle)) < 0) {
-         error ("Can't prepare after reset: %s", snd_strerror(err));
-         return 0;
+      if (!handle) {
+         logit ("alsa_reset() when the device is not opened.");
+         break;
+      }
+
+      rc = snd_pcm_drop (handle);
+      if (rc < 0) {
+         error ("Can't reset the device: %s", snd_strerror (rc));
+         break;
+      }
+
+      rc = snd_pcm_prepare (handle);
+      if (rc < 0) {
+         error ("Can't prepare after reset: %s", snd_strerror (rc));
+         break;
       }
 
       alsa_buf_fill = 0;
-   }
-   else
-      logit ("alsa_reset() when the device is not opened.");
-   return 1;
+      result = 1;
+   } while (0);
+
+   return result;
 }
 
 static int alsa_get_rate ()
@@ -745,9 +840,14 @@ static void alsa_toggle_mixer_channel ()
 
 static char *alsa_get_mixer_channel_name ()
 {
+   char *result;
+
    if (mixer_elem_curr == mixer_elem1)
-      return xstrdup (options_get_str ("ALSAMixer1"));
-   return xstrdup (options_get_str ("ALSAMixer2"));
+      result = xstrdup (options_get_str ("ALSAMixer1"));
+   else
+      result = xstrdup (options_get_str ("ALSAMixer2"));
+
+   return result;
 }
 
 void alsa_funcs (struct hw_funcs *funcs)
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Multimedia All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum