]> granicus.if.org Git - nethack/commitdiff
fix achievement sounds in the macsound interface
authornhmall <nhmall@nethack.org>
Fri, 3 Mar 2023 01:30:05 +0000 (20:30 -0500)
committernhmall <nhmall@nethack.org>
Fri, 3 Mar 2023 01:30:05 +0000 (20:30 -0500)
> With sounds on, using #levelchange to drop more than one level only
> gave the level-loss sound effect once.

Fix the underlying issue within macsound.m by allowing the currently
playing sound to run to its completion prior to starting the next.

This also backs out the acknowledgment --More-- that was added after
the sound issue first materialized and puts it back to the way it was
the past several releases.

sound/macsound/macsound.m

index ffc83bbfe8001036ff3d2ef770376016a187972d..86a353275d69674fc0f5279433f817436aa806a3 100644 (file)
@@ -2,6 +2,11 @@
 /* Copyright Michael Allison, 2023 */
 /* NetHack may be freely redistributed.  See license for details. */
 
+
+/*
+ * macsound
+ */
+
 #include "hack.h"
 
 #ifdef DEBUG
 #import <AVFoundation/AVFoundation.h>
 #endif
 
-
-/*
- * Sample sound interface for NetHack
- *
- * Replace 'macsound' with your soundlib name in this template file.
- * Should be placed in ../sound/macsound/.
- */
-
-#define soundlib_macsound soundlib_nosound + 1
-
 static void macsound_init_nhsound(void);
 static void macsound_exit_nhsound(const char *);
 static void macsound_achievement(schar, schar, int32_t);
@@ -156,6 +151,9 @@ static void macsound_hero_playnotes(int32_t instrument WAVEMUSICONLY,
     char resourcename[120], *end_of_res = 0;
     const char *c = 0;
     float fvolume = (float) vol / 100.00;
+    int time_remaining = 0;
+    NSTimeInterval dur;
+    NSTimeInterval elapsed;
 
     if (fvolume < 0.1 || fvolume > 1.0)
         fvolume = 1.0;
@@ -237,16 +235,24 @@ static void macsound_hero_playnotes(int32_t instrument WAVEMUSICONLY,
                 affiliate(pseudo_seid, resourcename);
             }
             if (affiliation[pseudo_seid]) {
+                if ([seSound[pseudo_seid] isPlaying]) {
+                    dur = [seSound[pseudo_seid] duration];
+                    elapsed = [seSound[pseudo_seid] currentTime];
+                    time_remaining = (int) ((dur - elapsed) * 1000);
+                    msleep(time_remaining);
+                }
                 if ([seSound[pseudo_seid] isPlaying])
                     [seSound[pseudo_seid] stop];
                 if ([seSound[pseudo_seid] volume] != fvolume)
                     [seSound[pseudo_seid] setVolume:fvolume];
                 [seSound[pseudo_seid] play];
+#if 0
                 if (i < notecount - 1) {
                     /* more notes to follow */
                     msleep(150);
                     [seSound[pseudo_seid] stop];
                 }
+#endif
             }
         }
         c++;
@@ -271,6 +277,9 @@ int32_t moreinfo UNUSED)
 #ifdef ACHIEVEMENT_SOUNDS
     char resourcename[120];
     uint32_t pseudo_seid, pseudo_seid_base;
+    int time_remaining = 0;
+    NSTimeInterval dur;
+    NSTimeInterval elapsed;
 
     if (ach1 == 0 && ach2 == 0)
         return;
@@ -309,8 +318,15 @@ int32_t moreinfo UNUSED)
                 affiliate(pseudo_seid, resourcename);
             }
             if (affiliation[pseudo_seid]) {
-                if ([seSound[pseudo_seid] isPlaying])
+                if ([seSound[pseudo_seid] isPlaying]) {
+                    dur = [seSound[pseudo_seid] duration];
+                    elapsed = [seSound[pseudo_seid] currentTime];
+                    time_remaining = (int) ((dur - elapsed) * 1000);
+                    msleep(time_remaining);
+                }
+                if ([seSound[pseudo_seid] isPlaying]) {
                     [seSound[pseudo_seid] stop];
+                }
                 [seSound[pseudo_seid] play];
             }
         }