Ejemplo n.º 1
0
 public void Play(bool onFlag)
 {
     // looping sounds don't get restarted
     if (looping)
     {
         if (onFlag)
         {
             if (!lastValue)
             {
                 buffer.SetCurrentPosition(1000);
                 buffer.Play(0, BufferPlayFlags.Looping);
             }
         }
         else
         {
             buffer.Stop();
         }
         lastValue = onFlag;
     }
     else
     {
         if (onFlag)
         {
             buffer.SetCurrentPosition(0);
             buffer.Play(0, BufferPlayFlags.Default);
         }
     }
 }
Ejemplo n.º 2
0
 private void HaltDXSound(Buffer buffer)
 {
     if (ProblemWithSound)
     {
         return;
     }
     try
     {
         buffer.Stop();
     }
     catch (Exception)
     {
         ProblemWithSound = true;
     }
 }
        /// <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;
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Stop the playback of the sound.
 /// </summary>
 public void Stop()
 {
     buffer.Stop();
 }