Beispiel #1
0
    /// <summary>
    /// Fills the buffer with new data.
    /// </summary>
    /// <param name="index">The starting index from where to fill the buffer.</param>
    /// <param name="data">The new content of the buffers.</param>
    public void FillBuffer(int index, SoundBufferData data)
    {
        if (index < 0 || index >= handles.Length)
        {
            throw new ArgumentOutOfRangeException(nameof(index));
        }

        if (data.Buffers.Count > handles.Length)
        {
            throw new ArgumentException("This data does not fit in the buffer.", nameof(data));
        }

        for (var i = 0; i < data.Buffers.Count; i++)
        {
            fillBufferRaw((index + i) % handles.Length, data.Buffers[i], data.Format, data.SampleRate);
        }
    }
Beispiel #2
0
    private static void playPewSound()
    {
        Console.WriteLine("Playing pew.");

        // Load the data from file into memory.
        // Note that this process decompresses the sound data fully. Don't use this synchronously, especially on
        // large audio files. Use streaming instead.
        var data = SoundBufferData.FromWav("assets/pew.wav");

        // Initializes a sound buffer with the data loaded from the file. This copies the data into the sound card
        // or reserved memory for the software implementation of OpenAL.
        var buffer = new SoundBuffer(data);

        // Initialize a new audio source. An audio source can be persistent and play multiple sounds.
        // A source could for example be an object in the game world that makes sounds.
        // Note that the number of sources existing simultaneously is limited.
        var source = new Source();

        // Tell the source about the buffers it should play from.
        source.QueueBuffer(buffer);

        // Start playing the source. This will play the queued buffers in orders.
        source.Play();

        // Wait until the source finishes playing all the buffers.
        while (!source.FinishedPlaying)
        {
            Thread.Sleep(100);
        }

        // The source will keep the buffers in its queue. This means you can rewind the source if you want.
        // However, if you want to reuse the source for other files, you should dequeue the already played buffers.
        // You don't have to wait until the source finishes playing with calling this. For example streaming would
        // dequeue finished buffers and queue new buffers at the end.
        source.DequeueProcessedBuffers();

        // Since we are done with this source, we dispose it. This clears up a slot for a new source.
        source.Dispose();

        Console.WriteLine("Finished pew.");
    }
Beispiel #3
0
 /// <summary>
 /// Fills the buffer with new data.
 /// </summary>
 /// <param name="data">The new content of the buffers.</param>
 public void FillBuffer(SoundBufferData data)
 {
     FillBuffer(0, data);
 }
Beispiel #4
0
 /// <summary>
 /// Generates a news sound buffer and fills it.
 /// </summary>
 /// <param name="data">The data to load the buffers with.</param>
 public SoundBuffer(SoundBufferData data)
     : this(data.Buffers.Count)
 {
     FillBuffer(data);
 }