//file,data streams

        unsafe void BeginStreamPlay()
        {
            //clear buffer
            {
                void *lockedBuffer     = null;
                uint  lockedBufferSize = 0;

                int hr = IDirectSoundBuffer.Lock(currentSoundBuffer, 0, (uint)currentSound.bufferSize,
                                                 &lockedBuffer, &lockedBufferSize, (void **)null, (uint *)null, 0);
                if (Wrapper.FAILED(hr))
                {
                    DirectSoundWorld.Warning("IDirectSoundBuffer.Lock", hr);
                    return;
                }

                NativeUtils.FillMemory((IntPtr)lockedBuffer, (int)lockedBufferSize,
                                       (byte)(currentSound.waveFormat->wBitsPerSample == 8 ? 128 : 0));

                IDirectSoundBuffer.Unlock(currentSoundBuffer, lockedBuffer, lockedBufferSize, null, 0);
            }

            UpdateStreamBuffer(true);
            UpdateStreamBuffer(false);
            streamNeedWriteFirstPart = true;
            needStopAfterBufferRead  = false;
        }
Ejemplo n.º 2
0
        unsafe public bool FillSoundBuffersWithData()
        {
            IDirectSoundBuffer *soundBuffer = (IDirectSoundBuffer *)soundBuffers[0].ToPointer();

            int hr;

            void *lockedBuffer     = null;
            uint  lockedBufferSize = 0;

            hr = IDirectSoundBuffer.Lock(soundBuffer, 0, (uint)soundSamples.Length,
                                         &lockedBuffer, &lockedBufferSize, (void **)null, (uint *)null, 0);

            if (Wrapper.FAILED(hr))
            {
                DirectSoundWorld.Warning("IDirectSoundBuffer.Lock", hr);
                return(false);
            }

            if ((int)lockedBufferSize < soundSamples.Length)
            {
                Log.Fatal("DirectSampleSound.FillSoundBuffersWithData: " +
                          "lockedBufferSize >= soundSamples->Length");
            }

            Marshal.Copy(soundSamples, 0, (IntPtr)lockedBuffer, soundSamples.Length);

            if (soundSamples.Length < (int)lockedBufferSize)
            {
                // fill with silence remaining bytes
                NativeUtils.FillMemory((IntPtr)((byte *)lockedBuffer + soundSamples.Length),
                                       (int)lockedBufferSize - soundSamples.Length,
                                       (byte)(waveFormat->wBitsPerSample == 8 ? 128 : 0));
            }

            IDirectSoundBuffer.Unlock(soundBuffer, lockedBuffer, lockedBufferSize, (void *)null, 0);

            return(true);
        }
        unsafe void UpdateStreamBuffer(bool firstPart)
        {
            DirectFileStreamSound currentFileStreamSound = currentSound as DirectFileStreamSound;

            void *lockedBuffer     = null;
            uint  lockedBufferSize = 0;

            int hr = IDirectSoundBuffer.Lock(currentSoundBuffer,
                                             (uint)(firstPart ? 0 : currentSound.bufferSize / 2),
                                             (uint)(currentSound.bufferSize / 2), &lockedBuffer, &lockedBufferSize,
                                             (void **)null, (uint *)null, 0);

            if (Wrapper.FAILED(hr))
            {
                DirectSoundWorld.Warning("IDirectSoundBuffer.Lock", hr);
                return;
            }

            if ((int)lockedBufferSize < currentSound.bufferSize / 2)
            {
                Log.Fatal("DirectSoundRealChannel.UpdateStreamBuffer: " +
                          "lockedBufferSize >= currentSound->bufferSize / 2.");
            }

            bool repeated = false;

            int readed = 0;

again:

            if (currentFileStreamSound != null)
            {
                readed += ReadDataFromFileStream((IntPtr)((byte *)lockedBuffer + readed),
                                                 currentSound.bufferSize / 2 - readed);
            }
            else
            {
                readed += ReadDataFromDataStream((IntPtr)((byte *)lockedBuffer + readed),
                                                 currentSound.bufferSize / 2 - readed);
            }

            if (readed < currentSound.bufferSize / 2)
            {
                NativeUtils.FillMemory((IntPtr)((byte *)lockedBuffer + readed),
                                       (int)lockedBufferSize - readed,
                                       (byte)(currentSound.waveFormat->wBitsPerSample == 8 ? 128 : 0));

                if ((int)(currentSound.Mode & SoundMode.Loop) != 0)
                {
                    if (currentFileStreamSound != null)
                    {
                        //loop play. we need recreate vorbis file
                        currentFileStreamSound.Rewind();

                        if (!repeated)
                        {
                            repeated = true;
                            goto again;
                        }
                    }
                }
            }

            if (readed == 0)
            {
                //need stop
                if ((int)(currentSound.Mode & SoundMode.Loop) == 0)
                {
                    if (currentFileStreamSound != null)
                    {
                        needStopAfterBufferRead = true;
                    }
                }
            }

            IDirectSoundBuffer.Unlock(currentSoundBuffer, lockedBuffer, lockedBufferSize, null, 0);
        }