# Ralf Fassel posted to comp.lang.tcl on 2023-01-09.

--- snack2.2.10/generic/jkSoundEngine.c.orig
+++ snack2.2.10/generic/jkSoundEngine.c
@@ -1694,7 +1694,7 @@
 
   if (soundQueue != NULL) {
     for (p = soundQueue; p != NULL && p->sound != s; p = p->next);
-    if (p->sound == s) {
+    if (p && p->sound == s) {
       n = p->startPos + p->nWritten;
     }
   }

==================================================


# https://bugs.gentoo.org/show_bug.cgi?id=226137
# + double-close-protection
--- snack2.2.10/unix/jkAudIO_alsa.c.orig
+++ snack2.2.10/unix/jkAudIO_alsa.c
@@ -49,6 +49,8 @@
 
 static int minNumChan = 1;
 
+static snd_pcm_uframes_t hw_bufsize = 0;
+
 int
 SnackAudioOpen(ADesc *A, Tcl_Interp *interp, char *device, int mode, int freq,
 	       int nchannels, int encoding)
@@ -135,6 +137,9 @@
     Tcl_AppendResult(interp, "Failed setting HW params.", NULL);
     return TCL_ERROR;
   }
+
+  snd_pcm_hw_params_get_buffer_size (hw_params, &hw_bufsize);
+
   snd_pcm_hw_params_free(hw_params);
   snd_pcm_prepare(A->handle);
   if (A->mode == RECORD) {
@@ -155,9 +160,11 @@
 {
   if (A->debug > 1) Snack_WriteLog("  Enter SnackAudioClose\n");
 
-  snd_pcm_drop(A->handle);
-  snd_pcm_close(A->handle);
-
+  if (A->handle) {
+    snd_pcm_drop(A->handle);
+    snd_pcm_close(A->handle);
+    A->handle = NULL;
+  }
   if (A->debug > 1) Snack_WriteLog("  Exit SnackAudioClose\n");
 
   return(0);
@@ -202,6 +209,8 @@
   int i;
   static char buf[64];
 
+  return;
+      
   if (A->debug > 1) Snack_WriteLog("  Enter SnackAudioPost\n");
 
   for (i = 0; i < 1000; i++) {
@@ -267,12 +276,14 @@
 long
 SnackAudioPlayed(ADesc *A)
 {
-  long avail = _snd_pcm_mmap_hw_ptr(A->handle);
+  // FIX Here, _snd_pcm_mmap_hw_ptr is deprecated in new alsalib
+  long played = A->nWritten - (hw_bufsize - SnackAudioWriteable(A));
+  // long avail = _snd_pcm_mmap_hw_ptr(A->handle);
   
-  if (avail < 0)
-    avail = 0;
+  if (played < 0)
+   return 0;
 
-  return (avail+A->nPlayed);
+  return (played);
 }
 
 void
