SoundStream.st
changeset 715 b2950d207977
parent 714 ebeacc2e07bc
child 720 967fad20a8f1
equal deleted inserted replaced
714:ebeacc2e07bc 715:b2950d207977
  1408       if ((oDirectSound = __INST(pDirectSound)) != nil) {
  1408       if ((oDirectSound = __INST(pDirectSound)) != nil) {
  1409 	  t_pDirectSound = __DirectSoundVal(oDirectSound);
  1409 	  t_pDirectSound = __DirectSoundVal(oDirectSound);
  1410       }
  1410       }
  1411 
  1411 
  1412       if (!t_pDSBuffer || !t_pDirectSound) {
  1412       if (!t_pDSBuffer || !t_pDirectSound) {
       
  1413 	  fprintf(stderr, "SoundStream not open!\n");
  1413 	  RETURN (0);
  1414 	  RETURN (0);
  1414       }
  1415       }
  1415       t_cbBufOffset = __intVal(__INST(cbBufOffset));
  1416       t_cbBufOffset = __intVal(__INST(cbBufOffset));
  1416       t_cbBufSize = __intVal(__INST(cbBufSize));
  1417       t_cbBufSize = __intVal(__INST(cbBufSize));
  1417 
  1418 
  1420       buf = (short *)__InstPtr(anObject) + OHDR_SIZE + offs;
  1421       buf = (short *)__InstPtr(anObject) + OHDR_SIZE + offs;
  1421 
  1422 
  1422       // Should be playing, right?
  1423       // Should be playing, right?
  1423       hr = IDirectSoundBuffer_GetStatus(t_pDSBuffer, &status );
  1424       hr = IDirectSoundBuffer_GetStatus(t_pDSBuffer, &status );
  1424       if (!(status && DSBSTATUS_PLAYING)) {
  1425       if (!(status && DSBSTATUS_PLAYING)) {
  1425 	  printf("Buffer not playing!\n");
  1426 	  fprintf(stderr, "Buffer not playing!\n");
       
  1427 	  RETURN (0);
  1426       }
  1428       }
  1427 
  1429 
  1428       // Sleep until we have enough room in buffer.
  1430       // Sleep until we have enough room in buffer.
  1429       hr = IDirectSoundBuffer_GetCurrentPosition(t_pDSBuffer, &playPos, &safePos );
  1431       hr = IDirectSoundBuffer_GetCurrentPosition(t_pDSBuffer, &playPos, &safePos );
  1430       if( hr != DS_OK ) {
  1432       if( hr != DS_OK ) {
       
  1433 	  fprintf(stderr, "Cannot get position!\n");
  1431 	  RETURN (0);
  1434 	  RETURN (0);
  1432       }
  1435       }
  1433       if( playPos < t_cbBufOffset ) playPos += t_cbBufSize; 
  1436       if( playPos < t_cbBufOffset ) playPos += t_cbBufSize; 
  1434 
  1437 
  1435       endWrite = t_cbBufOffset + (cnt * sizeof(short));
  1438       endWrite = t_cbBufOffset + (cnt * sizeof(short));
  1443 	  Sleep( millis );
  1446 	  Sleep( millis );
  1444 
  1447 
  1445 	  // Wake up, find out where we are now
  1448 	  // Wake up, find out where we are now
  1446 	  hr = IDirectSoundBuffer_GetCurrentPosition(t_pDSBuffer, &playPos, &safePos );
  1449 	  hr = IDirectSoundBuffer_GetCurrentPosition(t_pDSBuffer, &playPos, &safePos );
  1447 	  if( hr != DS_OK ) {
  1450 	  if( hr != DS_OK ) {
       
  1451 	      fprintf(stderr, "Cannot get position!\n");
  1448 	      RETURN (0);
  1452 	      RETURN (0);
  1449 	  }
  1453 	  }
  1450 	  if( playPos < t_cbBufOffset ) playPos += t_cbBufSize; // unwrap offset
  1454 	  if( playPos < t_cbBufOffset ) playPos += t_cbBufSize; // unwrap offset
  1451       }
  1455       }
  1452 
  1456 
  1461 	  t_cbBufOffset = (t_cbBufOffset + dwsize1 + dwsize2) % t_cbBufSize;
  1465 	  t_cbBufOffset = (t_cbBufOffset + dwsize1 + dwsize2) % t_cbBufSize;
  1462 	  IDirectSoundBuffer_Unlock(t_pDSBuffer, lpbuf1, dwsize1, lpbuf2, dwsize2);
  1466 	  IDirectSoundBuffer_Unlock(t_pDSBuffer, lpbuf1, dwsize1, lpbuf2, dwsize2);
  1463       }
  1467       }
  1464       __INST(cbBufOffset) = __MKSMALLINT(t_cbBufOffset);
  1468       __INST(cbBufOffset) = __MKSMALLINT(t_cbBufOffset);
  1465 
  1469 
  1466       RETURN (0);
  1470       RETURN (count);
  1467   }
  1471   }
  1468 #endif /* WIN32 */
  1472 #endif /* WIN32 */
  1469 
  1473 
  1470 #if defined(DEV_AUDIO)
  1474 #if defined(DEV_AUDIO)
  1471    /*
  1475    /*
  1725     }
  1729     }
  1726 
  1730 
  1727     __INST(cbBufOffset) = __MKSMALLINT(0);  // reset last write position to start of buffer
  1731     __INST(cbBufOffset) = __MKSMALLINT(0);  // reset last write position to start of buffer
  1728 
  1732 
  1729     /* Start the buffer playback */
  1733     /* Start the buffer playback */
  1730     if ((result = IDirectSoundBuffer_Play(t_pDSBuffer, 0, 0, DSBPLAY_LOOPING ) != DS_OK)) {
  1734     if ((result = IDirectSoundBuffer_Play(t_pDSBuffer, 0, 0, DSBPLAY_LOOPING) != DS_OK)) {
  1731 	fprintf(stderr,"SoundStream: couldn't play sound buffer!\n");
  1735 	fprintf(stderr,"SoundStream: couldn't play sound buffer!\n");
  1732 	IDirectSoundBuffer_Stop(t_pDSBuffer);
  1736 	IDirectSoundBuffer_Stop(t_pDSBuffer);
  1733 	IDirectSoundBuffer_Stop(t_pDSPrimeBuffer);
  1737 	IDirectSoundBuffer_Stop(t_pDSPrimeBuffer);
  1734 	IDirectSoundBuffer_Release(t_pDSPrimeBuffer);
  1738 	IDirectSoundBuffer_Release(t_pDSPrimeBuffer);
  1735 	IDirectSound_Release(t_pDirectSound);
  1739 	IDirectSound_Release(t_pDirectSound);
  1753 
  1757 
  1754 !SoundStream methodsFor:'sine wave generation'!
  1758 !SoundStream methodsFor:'sine wave generation'!
  1755 
  1759 
  1756 tuneTone
  1760 tuneTone
  1757     audioFormat == #S16 ifTrue:[
  1761     audioFormat == #S16 ifTrue:[
  1758         ^ self tuneTone16:440
  1762 	^ self tuneTone16:440
  1759     ].
  1763     ].
  1760     audioFormat == #U16 ifTrue:[
  1764     audioFormat == #U16 ifTrue:[
  1761         ^ self tuneTone16:440
  1765 	^ self tuneTone16:440
  1762     ].
  1766     ].
  1763     audioFormat == #MU_LAW ifTrue:[
  1767     audioFormat == #MU_LAW ifTrue:[
  1764         ^ self tuneToneMU:440
  1768 	^ self tuneToneMU:440
  1765     ].
  1769     ].
  1766     self tuneTone:440
  1770     self tuneTone:440
  1767 
  1771 
  1768     "
  1772     "
  1769      SoundStream writing tuneTone; close
  1773      SoundStream writing tuneTone; close
  1789 
  1793 
  1790     "fill it with a sine wave"
  1794     "fill it with a sine wave"
  1791 
  1795 
  1792     scale := freq * 2 * (Float pi).
  1796     scale := freq * 2 * (Float pi).
  1793     1 to:numSamples do:[:i |
  1797     1 to:numSamples do:[:i |
  1794         val := (scale * i / numSamples) sin.
  1798 	val := (scale * i / numSamples) sin.
  1795         val := (val * 16r7FFF) rounded bitAnd:16rFFFF.
  1799 	val := (val * 16r7FFF) rounded bitAnd:16rFFFF.
  1796         audioFormat == #U16 ifTrue:[
  1800 	audioFormat == #U16 ifTrue:[
  1797             val := val + 32768
  1801 	    val := val + 32768
  1798         ].
  1802 	].
  1799         buffer at:i put:val
  1803 	buffer at:i put:val
  1800     ].
  1804     ].
  1801 
  1805 
  1802     oldFormat := audioFormat.
  1806     oldFormat := audioFormat.
  1803     self setAudioFormat:#S16.
  1807     self setAudioFormat:#S16.
  1804     1 to:3 do:[:s |
  1808     1 to:3 do:[:s |
  1805         self nextPutBytes:(numSamples*2) from:buffer startingAt:1
  1809 	self nextPutBytes:(numSamples*2) from:buffer startingAt:1
  1806     ].
  1810     ].
  1807     self setAudioFormat:oldFormat.
  1811     self setAudioFormat:oldFormat.
  1808 
  1812 
  1809     "of course, the frequency should be below half the
  1813     "of course, the frequency should be below half the
  1810      sampleRate - hear below ...
  1814      sampleRate - hear below ...
  1908 ! !
  1912 ! !
  1909 
  1913 
  1910 !SoundStream class methodsFor:'documentation'!
  1914 !SoundStream class methodsFor:'documentation'!
  1911 
  1915 
  1912 version
  1916 version
  1913 ^ '$Header: /cvs/stx/stx/libbasic2/SoundStream.st,v 1.36 1998-12-23 15:09:58 cg Exp $'! !
  1917 ^ '$Header: /cvs/stx/stx/libbasic2/SoundStream.st,v 1.37 1998-12-23 15:40:09 cg Exp $'! !
  1914 SoundStream initialize!
  1918 SoundStream initialize!