Exemplo n.º 1
0
        protected virtual void Dispose(bool disposing)
        {
            // If you need thread safety, use a lock around these
            // operations, as well as in your methods that use the resource.
            if (!m_disposed)
            {
                if (disposing)
                {
                    if (m_descriptionListener != null)
                    {
                        m_descriptionListener.Dispose();
                    }

                    if (m_primary3d != null)
                    {
                        m_primary3d.Dispose();
                    }

                    if (m_listener3d != null)
                    {
                        m_listener3d.Dispose();
                    }
                }

                m_descriptionListener = null;
                m_primary3d           = null;
                m_listener3d          = null;

                // Indicate that the instance has been disposed.
                m_disposed = true;
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Cleans up resources
 /// </summary>
 public void Dispose()
 {
     SoundManager.RemoveSound(this);
     if (buffer != null)
     {
         buffer.Dispose();
         buffer = null;
     }
 }
        /// <summary>
        ///   Read an audio file using mono format
        /// </summary>
        /// <param name = "pathToFile">File to read from. Should be a .wav file</param>
        /// <param name = "sampleRate">
        ///   Sample rate of the file. This proxy does not support down or up sampling.
        ///   Please convert the file to appropriate sample, and then use this method.
        /// </param>
        /// <param name = "secondsToRead">Seconds to read</param>
        /// <param name = "startAtSecond">Start at second</param>
        /// <returns>Audio samples</returns>
        public override float[] ReadMonoFromFile(string pathToFile, int sampleRate, int secondsToRead, int startAtSecond)
        {
            int totalSeconds = secondsToRead <= 0 ? int.MaxValue : secondsToRead + startAtSecond;
            if (alreadyDisposed)
            {
                throw new ObjectDisposedException("Object already disposed");
            }

            if (Path.GetExtension(pathToFile) != ".wav")
            {
                throw new ArgumentException(
                    "DirectSound can read only .wav files. Please transform your input file into appropriate type.");
            }

            Device device = new Device(new DevicesCollection()[0].DriverGuid);
            Buffer buffer = new Buffer(Path.GetFullPath(pathToFile), device);

            /*Default sound card is used as parent Device*/
            long fileSize = buffer.Caps.BufferBytes;
            int offset = 0;
            int bytesPerSample = buffer.Format.BitsPerSample / 8;
            const int OutputBufferSize = 5512 * 10 * 4;
            List<float[]> chunks = new List<float[]>();
            int size = 0;
            try
            {
                while ((float)size / sampleRate < totalSeconds)
                {
                    byte[] ar = (byte[])buffer.Read(offset, typeof(byte), LockFlag.EntireBuffer, OutputBufferSize);
                    offset += OutputBufferSize;
                    long readData = offset > fileSize ? fileSize - (offset - OutputBufferSize) : ar.Length;
                    float[] result = new float[readData / bytesPerSample];
                    for (int i = 0; i < result.Length; i++)
                    {
                        switch (bytesPerSample)
                        {
                            case 2:
                                result[i] = BitConverter.ToInt16(ar, i * bytesPerSample);
                                break;
                            case 4:
                                result[i] = BitConverter.ToInt32(ar, i * bytesPerSample);
                                break;
                        }
                    }

                    chunks.Add(result);
                    size += result.Length;
                    if (offset > fileSize)
                    {
                        break;
                    }
                }
            }
            finally
            {
                buffer.Stop();
                buffer.Dispose();
                device.Dispose();
            }

            if ((float)size / sampleRate < (secondsToRead + startAtSecond))
            {
                return null; /*not enough samples to return the requested data*/
            }

            int start = (int)((float)startAtSecond * sampleRate);
            int end = (secondsToRead <= 0) ? size : (int)((float)(startAtSecond + secondsToRead) * sampleRate);
            float[] data = new float[size];
            int index = 0;
            /*Concatenate*/
            foreach (float[] chunk in chunks)
            {
                Array.Copy(chunk, 0, data, index, chunk.Length);
                index += chunk.Length;
            }

            /*Select specific part of the song*/
            if (start != 0 || end != size)
            {
                float[] temp = new float[end - start];
                Array.Copy(data, start, temp, 0, end - start);
                data = temp;
            }

            return data;
        }