Implement Sound Blaster digital audio playback
authorRobert Pengelly <robertapengelly@hotmail.com>
Thu, 29 Oct 2026 10:41:04 +0000 (10:41 +0000)
committerRobert Pengelly <robertapengelly@hotmail.com>
Thu, 29 Oct 2026 10:41:04 +0000 (10:41 +0000)
cpu.c
cpu.h
speaker.c
speaker.h

diff --git a/cpu.c b/cpu.c
index df19d6b55f7501036ae178a746bea10a76e71dc0..228aee15d60de9447646e987042a96483d31dbff 100644 (file)
--- a/cpu.c
+++ b/cpu.c
@@ -1468,36 +1468,43 @@ static void sb_dma_update (CPU8086 *cpu) {
 
     DMA8237 *dma = &cpu->dma[0];
     
-    uint64_t now;
-    unsigned bytes;
+    uint8_t samples[4096];
+    uint64_t now, period;
+    
+    unsigned bytes, transferred = 0;
     
     if (!cpu->sb_dsp_dma_active || cpu->sb_dsp_dma_paused || !cpu->sb_dsp_block_length || (dma->mask & 2) || (dma->mode[1] & 0x0F) != 0x09) {
         return;
     }
     
     now = pit_clock_ns ();
+    period = (uint64_t) (256 - cpu->sb_dsp_time_constant) * 1000ULL;
     
-    if (now < cpu->sb_dsp_next_block_ns) {
+    if (now < cpu->sb_dsp_next_sample_ns) {
         return;
     }
     
-    bytes = cpu->sb_dsp_block_length;
+    bytes = (unsigned) ((now - cpu->sb_dsp_next_sample_ns) / period + 1);
     
-    while (bytes) {
+    if (bytes > sizeof (samples)) {
     
-        unsigned remaining = (unsigned) dma->count[1] + 1;
-        unsigned transferred = bytes < remaining ? bytes : remaining;
+        bytes = sizeof (samples);
+        cpu->sb_dsp_next_sample_ns = now - (bytes - 1) * period;
+    
+    }
+    
+    while (transferred < bytes) {
+    
+        uint32_t addr = ((uint32_t) dma->page[1] << 16) | dma->address[1];
+        samples[transferred++] = addr < cpu->mem_size ? cpu->mem[addr] : 0x80;
         
         if (dma->mode[1] & 0x20) {
-            dma->address[1] = (uint16_t) (dma->address[1] - transferred);
+            dma->address[1]--;
         } else {
-            dma->address[1] = (uint16_t) (dma->address[1] + transferred);
+            dma->address[1]++;
         }
         
-        dma->count[1] = (uint16_t) (dma->count[1] - transferred);
-        bytes -= transferred;
-        
-        if (transferred == remaining) {
+        if (dma->count[1]-- == 0) {
         
             if (dma->mode[1] & 0x10) {
             
@@ -1507,24 +1514,45 @@ static void sb_dma_update (CPU8086 *cpu) {
             } else {
             
                 dma->mask |= 2;
-                
                 cpu->sb_dsp_dma_active = 0;
-                break;
             
             }
         
         }
+        
+        if (++cpu->sb_dsp_block_progress == cpu->sb_dsp_block_length) {
+        
+            cpu->sb_dsp_block_progress = 0;
+            cpu->sb_dsp_irq_pending = 1;
+            cpu->pic[0].irr |= 0x80;
+            
+            if (!cpu->sb_dsp_dma_auto) {
+                cpu->sb_dsp_dma_active = 0;
+            }
+        
+        }
+        
+        if (!cpu->sb_dsp_dma_active || (dma->mask & 2)) {
+            break;
+        }
     
     }
     
-    cpu->sb_dsp_irq_pending = 1;
-    cpu->pic[0].irr |= 0x80;
+    cpu->sb_dsp_next_sample_ns += (uint64_t) transferred * period;
     
-    if (!cpu->sb_dsp_dma_auto) {
-        cpu->sb_dsp_dma_active = 0;
+    if (cpu->sb_dsp_speaker) {
+        speaker_sb_write (samples, transferred, (unsigned) (1000000ULL / (256 - cpu->sb_dsp_time_constant)));
     }
-    
-    cpu->sb_dsp_next_block_ns = now + (uint64_t) cpu->sb_dsp_block_length * (256 - cpu->sb_dsp_time_constant) * 1000ULL;
+
+}
+
+static void sb_dma_start (CPU8086 *cpu, int auto_init) {
+
+    cpu->sb_dsp_dma_active = 1;
+    cpu->sb_dsp_dma_auto = auto_init;
+    cpu->sb_dsp_dma_paused = 0;
+    cpu->sb_dsp_block_progress = 0;
+    cpu->sb_dsp_next_sample_ns = pit_clock_ns () + (uint64_t) (256 - cpu->sb_dsp_time_constant) * 1000ULL;
 
 }
 
@@ -2330,6 +2358,9 @@ static void io_write8 (CPU8086 *cpu, uint16_t port, uint8_t value) {
                 cpu->sb_dsp_dma_paused = 0;
                 cpu->sb_dsp_irq_pending = 0;
                 cpu->sb_dsp_param_count = 0;
+                cpu->sb_dsp_block_progress = 0;
+                
+                speaker_sb_reset ();
             
             } else if (cpu->sb_dsp_reset) {
             
@@ -2355,10 +2386,7 @@ static void io_write8 (CPU8086 *cpu, uint16_t port, uint8_t value) {
                     } else if (cpu->sb_dsp_command == 0x14) {
                     
                         cpu->sb_dsp_block_length = (uint32_t) (cpu->sb_dsp_params[0] | ((uint16_t) cpu->sb_dsp_params[1] << 8)) + 1;
-                        cpu->sb_dsp_dma_active = 1;
-                        cpu->sb_dsp_dma_auto = 0;
-                        cpu->sb_dsp_dma_paused = 0;
-                        cpu->sb_dsp_next_block_ns = pit_clock_ns () + (uint64_t) cpu->sb_dsp_block_length * (256 - cpu->sb_dsp_time_constant) * 1000ULL;
+                        sb_dma_start (cpu, 0);
                     
                     }
                 
@@ -2382,11 +2410,7 @@ static void io_write8 (CPU8086 *cpu, uint16_t port, uint8_t value) {
                 
                 case 0x1C:              /* Auto-initialize 8-bit DMA output */
                 
-                    cpu->sb_dsp_dma_active = 1;
-                    cpu->sb_dsp_dma_auto = 1;
-                    cpu->sb_dsp_dma_paused = 0;
-                    cpu->sb_dsp_next_block_ns = pit_clock_ns () + (uint64_t) cpu->sb_dsp_block_length * (256 - cpu->sb_dsp_time_constant) * 1000ULL;
-                    
+                    sb_dma_start (cpu, 1);
                     break;
                 
                 case 0xD0:              /* Halt 8-bit DMA */
@@ -2407,8 +2431,8 @@ static void io_write8 (CPU8086 *cpu, uint16_t port, uint8_t value) {
                 case 0xD4:              /* Continue 8-bit DMA */
                 
                     cpu->sb_dsp_dma_paused = 0;
-                    cpu->sb_dsp_next_block_ns = pit_clock_ns () + (uint64_t) cpu->sb_dsp_block_length * (256 - cpu->sb_dsp_time_constant) * 1000ULL;
                     
+                    cpu->sb_dsp_next_sample_ns = pit_clock_ns () + (uint64_t) (256 - cpu->sb_dsp_time_constant) * 1000ULL;
                     break;
                 
                 case 0xD8:              /* Read DSP speaker status */
diff --git a/cpu.h b/cpu.h
index e2a020057210d25a547a2c0bacb53d0775d9e14a..f855b114a574e16f458a8a9f9d3cb96b633548a6 100644 (file)
--- a/cpu.h
+++ b/cpu.h
@@ -108,8 +108,9 @@ typedef struct __attribute__ ((packed)) CPU8086 {
     uint8_t sb_dsp_time_constant, sb_dsp_dma_active, sb_dsp_dma_auto, sb_dsp_dma_paused;
     
     uint8_t sb_dsp_irq_pending;
-    uint32_t sb_dsp_block_length;
-    uint64_t sb_dsp_next_block_ns;
+    
+    uint32_t sb_dsp_block_length, sb_dsp_block_progress;
+    uint64_t sb_dsp_next_sample_ns;
     
     uint8_t opl_index, opl_timer1, opl_timer2, opl_control, opl_status;
     uint64_t opl_timer1_started_ns, opl_timer2_started_ns;
index 0af93b92eb3ed7739c65cf8e815a9e5cbe287255..4d15702431ba48b238e6b0f9bf2096764e3509c5 100644 (file)
--- a/speaker.c
+++ b/speaker.c
@@ -25,8 +25,9 @@
 #define     SPEAKER_RATE        48000
 #define     SPEAKER_VOLUME      4096
 
-#if     defined (_WIN32) || defined (__APPLE__) || defined (__linux__)
+#define     SB_QUEUE_SAMPLES    65536
 
+#if     defined (_WIN32) || defined (__APPLE__) || defined (__linux__)
 typedef struct Speaker {
 
     double elapsed;
@@ -38,6 +39,9 @@ typedef struct Speaker {
 
 static Speaker speaker = { 0, 65536, 0, 0 };
 
+static int16_t sb_samples[SB_QUEUE_SAMPLES];
+static unsigned sb_head, sb_tail, sb_count, sb_phase;
+
 static void speaker_render (int16_t *samples, int count) {
     
     double step = PIT_FREQUENCY / SPEAKER_RATE;
@@ -75,7 +79,30 @@ static void speaker_render (int16_t *samples, int count) {
         
         }
         
-        samples[i] = enabled ? (int16_t) (output ? SPEAKER_VOLUME : -SPEAKER_VOLUME) : 0;
+        {
+        
+            int mixed = enabled ? (output ? SPEAKER_VOLUME : -SPEAKER_VOLUME) : 0;
+            
+            if (sb_count) {
+            
+                mixed += sb_samples[sb_tail];
+                
+                sb_tail = (sb_tail + 1) % SB_QUEUE_SAMPLES;
+                sb_count--;
+            
+            }
+            
+            if (mixed > 32767) {
+                mixed = 32767;
+            }
+            
+            if (mixed < -32768) {
+                mixed = -32768;
+            }
+            
+            samples[i] = (int16_t) mixed;
+        
+        }
         
         if (speaker.port61 & 1) {
         
@@ -663,3 +690,97 @@ void speaker_update (uint8_t port61, uint8_t mode, uint16_t reload, int restart)
 #endif
 
 }
+
+void speaker_sb_write (const uint8_t *samples, size_t count, unsigned rate) {
+
+#if     defined (_WIN32)
+    if (!speaker_device || !rate) {
+        return;
+    }
+    
+    EnterCriticalSection (&speaker_lock);
+#elif   defined (__APPLE__)
+    if (!speaker_queue || !rate) {
+        return;
+    }
+    
+    pthread_mutex_lock (&speaker_lock);
+#elif   defined (__linux__)
+    if (!speaker_running || !rate) {
+        return;
+    }
+    
+    pthread_mutex_lock (&speaker_lock);
+#else
+    (void) samples;
+    (void) count;
+    (void) rate;
+    
+    return;
+#endif
+
+#if     defined (_WIN32) || defined (__APPLE__) || defined (__linux__)
+    for (size_t i = 0; i < count; i++) {
+    
+        int16_t sample = (int16_t) (((int) samples[i] - 128) * 128);
+        sb_phase += SPEAKER_RATE;
+        
+        while (sb_phase >= rate) {
+        
+            sb_phase -= rate;
+            
+            if (sb_count < SB_QUEUE_SAMPLES) {
+            
+                sb_samples[sb_head] = sample;
+                
+                sb_head = (sb_head + 1) % SB_QUEUE_SAMPLES;
+                sb_count++;
+            
+            }
+        
+        }
+    
+    }
+#endif
+
+#if     defined (_WIN32)
+    LeaveCriticalSection (&speaker_lock);
+#elif   defined (__APPLE__) || defined (__linux__)
+    pthread_mutex_unlock (&speaker_lock);
+#endif
+
+}
+
+void speaker_sb_reset (void) {
+
+#if     defined (_WIN32)
+    if (!speaker_device) {
+        return;
+    }
+    
+    EnterCriticalSection (&speaker_lock);
+#elif   defined (__APPLE__)
+    if (!speaker_queue) {
+        return;
+    }
+    
+    pthread_mutex_lock (&speaker_lock);
+#elif   defined (__linux__)
+    if (!speaker_running) {
+        return;
+    }
+    
+    pthread_mutex_lock (&speaker_lock);
+#endif
+
+#if     defined (_WIN32) || defined (__APPLE__) || defined (__linux__)
+    sb_head = sb_tail = sb_count = sb_phase = 0;
+#endif
+
+#if     defined (_WIN32)
+    LeaveCriticalSection (&speaker_lock);
+#elif   defined (__APPLE__) || defined (__linux__)
+    pthread_mutex_unlock (&speaker_lock);
+#endif
+
+}
index fb82270660b3743e88c4c9ee6a2f7d96d99c47d6..63898bb0678d59d58b6015e05e0f29b91aa6a3bd 100644 (file)
--- a/speaker.h
+++ b/speaker.h
@@ -4,6 +4,7 @@
 #ifndef     _SPEAKER_H
 #define     _SPEAKER_H
 
+#include    <stddef.h>
 #include    <stdint.h>
 
 void speaker_initialize (void);
@@ -11,4 +12,7 @@ void speaker_shutdown (void);
 
 void speaker_update (uint8_t port61, uint8_t mode, uint16_t reload, int restart);
 
+void speaker_sb_write (const uint8_t *samples, size_t count, unsigned rate);
+void speaker_sb_reset (void);
+
 #endif      /* _SPEAKER_H */