コード例 #1
0
ファイル: AudioDriver.cs プロジェクト: zwiglm/NeoAxisEngine
        protected int ReadData(byte[] buffer, int bufferOffset, int length)
        {
            if (oggFile.EndOfFile)
            {
                return(0);
            }

            oggFile.criticalSection.Enter();

            try
            {
                if (!IsSoundRealPlaying())
                {
                    return(0);
                }

                int sourceLength = length;
                if (oggFile.Sound3D && vorbisInfo.channels == 2)
                {
                    sourceLength *= 2;
                }

                //suspended
                //!!!!!норм EngineTime юзать?
                double time = EngineApp.EngineTime;
                if (time - oggFile.LastUpdateTime > .6f)
                {
                    return(0);
                }

                //read from ogg
                while (bufferSize < sourceLength)
                {
                    oggFile.Decode(true, false);
                    if (oggFile.EndOfFile)
                    {
                        break;
                    }
                }

                if (bufferSize == 0)
                {
                    return(0);
                }

                if (sourceLength > bufferSize)
                {
                    sourceLength = bufferSize;
                }

                int returnLength = sourceLength;

                //copy to channel buffer
                if (oggFile.Sound3D && vorbisInfo.channels == 2)
                {
                    unsafe
                    {
                        fixed(byte *source = samplesBuffer, destination = buffer)
                        {
                            ushort *s = (ushort *)source;
                            ushort *d = (ushort *)(destination + bufferOffset);

                            for (int n = 0; n < sourceLength; n += 4)
                            {
                                *d = *s;
                                d++;
                                s += 2;
                            }
                        }
                    }

                    returnLength = sourceLength / 2;
                }
                else
                {
                    unsafe
                    {
                        fixed(byte *pSamplesBuffer = samplesBuffer, pBuffer = buffer)
                        {
                            NativeUtility.CopyMemory((IntPtr)(pBuffer + bufferOffset),
                                                     (IntPtr)pSamplesBuffer, sourceLength);
                        }
                    }
                    //Array.Copy( samplesBuffer, 0, buffer, bufferOffset, sourceLength );
                }

                //remove data from samplesBuffer
                {
                    unsafe
                    {
                        fixed(byte *pSamplesBuffer = samplesBuffer)
                        {
                            NativeUtility.MoveMemory((IntPtr)pSamplesBuffer,
                                                     (IntPtr)(pSamplesBuffer + sourceLength), bufferSize - sourceLength);
                        }
                    }
                    //Array.Copy( samplesBuffer, sourceLength, samplesBuffer, 0, bufferSize - sourceLength );

                    bufferSize -= sourceLength;
                }

                return(returnLength);
            }
            catch (Exception ex)
            {
                Log.Error("OggFile: Exception: " + ex.ToString());
                return(0);
            }
            finally
            {
                oggFile.criticalSection.Leave();
            }
        }