SoundStream.st
changeset 4193 6d9eec6e6436
parent 3846 7c9889bed912
child 4617 c3f514269170
--- a/SoundStream.st	Fri Nov 04 16:20:24 2016 +0100
+++ b/SoundStream.st	Fri Nov 04 16:30:09 2016 +0100
@@ -1706,22 +1706,22 @@
     ALport p;
 
     if ((port = __INST(alPort)) != nil) {
-	p = __ALportVal(port);
-	while (ALgetfilled(p) > 0) {
-	    sginap(1);
-	}
+        p = __ALportVal(port);
+        while (ALgetfilled(p) > 0) {
+            sginap(1);
+        }
     }
     RETURN(self);
 #endif /* IRIS_AUDIO */
 
 #if defined(DEV_AUDIO)
     if (__isSmallInteger(fd)) {
-	int f = __intVal(fd);
-	/* ... */
+        int f = __intVal(fd);
+        /* ... */
     }
 #endif /* DEV_AUDIO */
 %}.
-    "dont know how to wait on non-iris systems"
+    "don't know how to wait on non-iris systems"
     ^ self
 !
 
@@ -1843,7 +1843,7 @@
     "write count bytes from an object starting at index start.
      return the number of bytes written or nil on error.
      Redefined, since IRIS audio library cannot be used with stdio.
-     (at least I dont know). Use with ByteArrays only."
+     (at least I don't know). Use with ByteArrays only."
 
 %{
 #ifdef IRIS_AUDIO
@@ -1855,27 +1855,27 @@
     char *cp;
 
     if ((port = __INST(alPort)) != nil) {
-	if (__INST(mode) != @symbol(readonly)) {
-	    if (__bothSmallInteger(count, start)) {
-		cnt = __intVal(count);
-		offs = __intVal(start) - 1;
-		p = __ALportVal(port);
+        if (__INST(mode) != @symbol(readonly)) {
+            if (__bothSmallInteger(count, start)) {
+                cnt = __intVal(count);
+                offs = __intVal(start) - 1;
+                p = __ALportVal(port);
 
-		/*
-		 * compute number of samples
-		 */
-		objSize = _Size(anObject) - OHDR_SIZE;
-		if ( (offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs)) ) {
-		    cp = (char *)__InstPtr(anObject) + OHDR_SIZE + offs;
-		    if (__INST(bitsPerSample) == __MKSMALLINT(16))
-			nSamp = cnt / 2;
-		    else
-			nSamp = cnt;
-		    ALwritesamps(p, cp, cnt);
-		    RETURN ( count );
-		}
-	    }
-	}
+                /*
+                 * compute number of samples
+                 */
+                objSize = _Size(anObject) - OHDR_SIZE;
+                if ( (offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs)) ) {
+                    cp = (char *)__InstPtr(anObject) + OHDR_SIZE + offs;
+                    if (__INST(bitsPerSample) == __MKSMALLINT(16))
+                        nSamp = cnt / 2;
+                    else
+                        nSamp = cnt;
+                    ALwritesamps(p, cp, cnt);
+                    RETURN ( count );
+                }
+            }
+        }
     }
   }
 #endif /* IRIS_AUDIO */
@@ -1898,15 +1898,15 @@
     int cnt, offs;
 
     if ((oDSBuffer = __INST(pDSBuffer)) != nil) {
-	t_pDSBuffer = __DSBufferVal(oDSBuffer);
+        t_pDSBuffer = __DSBufferVal(oDSBuffer);
     }
     if ((oDirectSound = __INST(pDirectSound)) != nil) {
-	t_pDirectSound = __DirectSoundVal(oDirectSound);
+        t_pDirectSound = __DirectSoundVal(oDirectSound);
     }
 
     if (!t_pDSBuffer || !t_pDirectSound) {
-	console_fprintf(stderr, "SoundStream not open!\n");
-	RETURN (0);
+        console_fprintf(stderr, "SoundStream not open!\n");
+        RETURN (0);
     }
     t_cbBufOffset = __intVal(__INST(bufferOffset));
     t_cbBufSize = __intVal(__INST(bufferSize));
@@ -1918,47 +1918,47 @@
     // Should be playing, right?
     hr = IDirectSoundBuffer_GetStatus(t_pDSBuffer, &status );
     if (!(status && DSBSTATUS_PLAYING)) {
-	console_fprintf(stderr, "Buffer not playing!\n");
-	RETURN (0);
+        console_fprintf(stderr, "Buffer not playing!\n");
+        RETURN (0);
     }
 
     // Sleep until we have enough room in buffer.
     hr = IDirectSoundBuffer_GetCurrentPosition(t_pDSBuffer, &playPos, &safePos );
     if( hr != DS_OK ) {
-	console_fprintf(stderr, "Cannot get position!\n");
-	RETURN (0);
+        console_fprintf(stderr, "Cannot get position!\n");
+        RETURN (0);
     }
     if( playPos < t_cbBufOffset ) playPos += t_cbBufSize;
 
     endWrite = t_cbBufOffset + (cnt * sizeof(short));
     while ( playPos < endWrite ) {
-	// Calculate number of milliseconds until we will have room, as
-	// time = distance * (milliseconds/second) / ((bytes/sample) * (samples/second)),
-	// rounded up.
-	millis = (DWORD) (1.0 + ((endWrite - playPos) * 1000.0) / ( sizeof(short) * __intVal(__INST(sampleRate))));
+        // Calculate number of milliseconds until we will have room, as
+        // time = distance * (milliseconds/second) / ((bytes/sample) * (samples/second)),
+        // rounded up.
+        millis = (DWORD) (1.0 + ((endWrite - playPos) * 1000.0) / ( sizeof(short) * __intVal(__INST(sampleRate))));
 
-	// Sleep for that long
-	Sleep( millis );
+        // Sleep for that long
+        Sleep( millis );
 
-	// Wake up, find out where we are now
-	hr = IDirectSoundBuffer_GetCurrentPosition(t_pDSBuffer, &playPos, &safePos );
-	if( hr != DS_OK ) {
-	    console_fprintf(stderr, "Cannot get position!\n");
-	    RETURN (0);
-	}
-	if( playPos < t_cbBufOffset ) playPos += t_cbBufSize; // unwrap offset
+        // Wake up, find out where we are now
+        hr = IDirectSoundBuffer_GetCurrentPosition(t_pDSBuffer, &playPos, &safePos );
+        if( hr != DS_OK ) {
+            console_fprintf(stderr, "Cannot get position!\n");
+            RETURN (0);
+        }
+        if( playPos < t_cbBufOffset ) playPos += t_cbBufSize; // unwrap offset
     }
 
     // Lock free space in the DS
     hr = IDirectSoundBuffer_Lock(t_pDSBuffer, t_cbBufOffset, cnt * sizeof(short), &lpbuf1, &dwsize1, &lpbuf2, &dwsize2, 0);
     if (hr == DS_OK) {
-	// Copy the buffer into the DS
-	CopyMemory(lpbuf1, buf, dwsize1);
-	if(NULL != lpbuf2) CopyMemory(lpbuf2, buf+dwsize1, dwsize2);
+        // Copy the buffer into the DS
+        CopyMemory(lpbuf1, buf, dwsize1);
+        if(NULL != lpbuf2) CopyMemory(lpbuf2, buf+dwsize1, dwsize2);
 
-	// Update our buffer offset and unlock sound buffer
-	t_cbBufOffset = (t_cbBufOffset + dwsize1 + dwsize2) % t_cbBufSize;
-	IDirectSoundBuffer_Unlock(t_pDSBuffer, lpbuf1, dwsize1, lpbuf2, dwsize2);
+        // Update our buffer offset and unlock sound buffer
+        t_cbBufOffset = (t_cbBufOffset + dwsize1 + dwsize2) % t_cbBufSize;
+        IDirectSoundBuffer_Unlock(t_pDSBuffer, lpbuf1, dwsize1, lpbuf2, dwsize2);
     }
     __INST(buffferOffset) = __MKSMALLINT(t_cbBufOffset);
 
@@ -1977,7 +1977,7 @@
     OBJ oWaveHandle;
 
     if ((oWaveHandle = __INST(waveHandle)) == nil) {
-	RETURN(0);
+        RETURN(0);
     }
     t_waveHandle = __WaveHandleVal(oWaveHandle);
 
@@ -1986,50 +1986,50 @@
     buf = (short *)__InstPtr(anObject) + OHDR_SIZE + offs;
 
     while (dataLen > 0) {
-	if (free_list == NULL && total_buffers < MAXBUF) {
-	    /* Expand available buffer space */
-	    bp = (struct buf *)malloc(sizeof(struct buf));
-	    total_buffers++;
-	} else {
-	    if (free_list == NULL) {
-		/* We must wait for a free buffer */
-		while (free_list == NULL) {
-		    WaitForSingleObject(free_buffer_event, INFINITE);
-		}
-	    }
-	    EnterCriticalSection(&free_list_lock);
-	    bp = free_list;
-	    free_list = free_list->next;
-	    --free_buffers;
-	    LeaveCriticalSection(&free_list_lock);
-	    r = waveOutUnprepareHeader(t_waveHandle, &bp->hdr, sizeof(WAVEHDR));
-	    if (r != 0) {
-		console_printf("waveOutUnprepareHeader\n");
-		RETURN(self);
-	    }
-	}
-	len = min(dataLen, DATALEN);
-	bp->hdr.lpData = (char *)bp->data;
-	bp->hdr.dwBufferLength = len;
-	bp->hdr.dwBytesRecorded = len;
-	bp->hdr.dwUser = (INT)(bp);
-	bp->hdr.dwFlags = 0;
-	bp->hdr.dwLoops = 0;
-	r = waveOutPrepareHeader(t_waveHandle, &bp->hdr, sizeof(WAVEHDR));
-	if (r != 0) {
-	    console_printf("waveOutPrepareHeader\n");
-	    RETURN(self);
-	}
-	for (i = 0; i < len; i++) {
-	    bp->data[i] = buf[i];
-	}
-	r = waveOutWrite(t_waveHandle, &bp->hdr, sizeof(WAVEHDR));
-	if (r != 0) {
-	    console_printf("waveOutWrite\n");
-	    RETURN(self);
-	}
-	buf += len;
-	dataLen -= len;
+        if (free_list == NULL && total_buffers < MAXBUF) {
+            /* Expand available buffer space */
+            bp = (struct buf *)malloc(sizeof(struct buf));
+            total_buffers++;
+        } else {
+            if (free_list == NULL) {
+                /* We must wait for a free buffer */
+                while (free_list == NULL) {
+                    WaitForSingleObject(free_buffer_event, INFINITE);
+                }
+            }
+            EnterCriticalSection(&free_list_lock);
+            bp = free_list;
+            free_list = free_list->next;
+            --free_buffers;
+            LeaveCriticalSection(&free_list_lock);
+            r = waveOutUnprepareHeader(t_waveHandle, &bp->hdr, sizeof(WAVEHDR));
+            if (r != 0) {
+                console_printf("waveOutUnprepareHeader\n");
+                RETURN(self);
+            }
+        }
+        len = min(dataLen, DATALEN);
+        bp->hdr.lpData = (char *)bp->data;
+        bp->hdr.dwBufferLength = len;
+        bp->hdr.dwBytesRecorded = len;
+        bp->hdr.dwUser = (INT)(bp);
+        bp->hdr.dwFlags = 0;
+        bp->hdr.dwLoops = 0;
+        r = waveOutPrepareHeader(t_waveHandle, &bp->hdr, sizeof(WAVEHDR));
+        if (r != 0) {
+            console_printf("waveOutPrepareHeader\n");
+            RETURN(self);
+        }
+        for (i = 0; i < len; i++) {
+            bp->data[i] = buf[i];
+        }
+        r = waveOutWrite(t_waveHandle, &bp->hdr, sizeof(WAVEHDR));
+        if (r != 0) {
+            console_printf("waveOutWrite\n");
+            RETURN(self);
+        }
+        buf += len;
+        dataLen -= len;
     }
     RETURN (count);
   }
@@ -2053,47 +2053,47 @@
     int fd;
 
     if ((fp = __INST(handle)) != nil) {
-	f = __FILEVal(fp);
-	if (__INST(mode) != @symbol(readonly)) {
-	    if (__bothSmallInteger(count, start)) {
-		cnt = __intVal(count);
-		offs = __intVal(start) - 1;
+        f = __FILEVal(fp);
+        if (__INST(mode) != @symbol(readonly)) {
+            if (__bothSmallInteger(count, start)) {
+                cnt = __intVal(count);
+                offs = __intVal(start) - 1;
 
-		objSize = _Size(anObject) - OHDR_SIZE;
-		if ( (offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs)) ) {
-		    do {
-			cp = (char *)__InstPtr(anObject) + OHDR_SIZE + offs;
+                objSize = _Size(anObject) - OHDR_SIZE;
+                if ( (offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs)) ) {
+                    do {
+                        cp = (char *)__InstPtr(anObject) + OHDR_SIZE + offs;
 
-			n = cnt;
+                        n = cnt;
 /*                        if (n > 4096) n = 4096; */
 # ifdef LINUX
-			sigsetmask(~0);
+                        sigsetmask(~0);
 # endif
-			if (__INST(buffered) == true) {
-			    n = fwrite(cp, 1, n, f);
-			} else {
-			    fd = fileno(f);
-			    n = write(fd, cp, n);
-			}
+                        if (__INST(buffered) == true) {
+                            n = fwrite(cp, 1, n, f);
+                        } else {
+                            fd = fileno(f);
+                            n = write(fd, cp, n);
+                        }
 # ifdef LINUX
-			sigsetmask(0);
+                        sigsetmask(0);
 # endif
-			__BEGIN_INTERRUPTABLE__
-			__END_INTERRUPTABLE__
-			if (n > 0) {
-			    offs += n;
-			    cnt -= n;
-			} else {
-			    if (n < 0) {
-				console_fprintf(stderr, "write error: %d\n", __threadErrno);
-				RETURN (count);
-			    }
-			}
-		    } while (cnt);
-		}
-		RETURN (count);
-	    }
-	}
+                        __BEGIN_INTERRUPTABLE__
+                        __END_INTERRUPTABLE__
+                        if (n > 0) {
+                            offs += n;
+                            cnt -= n;
+                        } else {
+                            if (n < 0) {
+                                console_fprintf(stderr, "write error: %d\n", __threadErrno);
+                                RETURN (count);
+                            }
+                        }
+                    } while (cnt);
+                }
+                RETURN (count);
+            }
+        }
     }
 #endif /* DEV_AUDIO */