Example #1
0
        /// <summary>
        /// Play a sound from the sample file
        /// </summary>
        /// <param name="sampleIndex">sample index (starting from zero) in the file</param>
        public void PlaySound(int sampleIndex)
        {
            var asy = Task.Run(() =>
            {
                if ((Voice == null) || (Instrument == null))
                {
                    return;
                }

                ReloadBuffer(sampleIndex);
                CancelStop.Set();
                //Set the frequency ratio to the voice
                Voice.SetFrequencyRatio(1);


                //Start the play if needed
                if (!IsPlaying)
                {
                    Voice.Start();
                    Voice.SetVolume(1, XAudio2.CommitNow);
                }

                //update state
                IsPlaying = true;
            });
        }
Example #2
0
        /// <summary>
        /// Start to play a note, It will be stopped until you call "StopNote"
        /// </summary>
        /// <param name="notePitch"></param>
        public void PlayNote(int notePitch)
        {
            var asy = Task.Run(() =>
            {
                if ((Voice == null) || (Instrument == null))
                {
                    return;
                }

                //If we play another note
                if ((notePitch != PlayingPitch) || !IsPlaying || stopping)
                {
                    //Get the frequenc y of the pitch
                    var FrequencyRatio = XAudio2.SemitonesToFrequencyRatio(notePitch) / 2f;
                    //See if we should use another sample
                    var reloadNeeded = Instrument.ReloadNeeded(FrequencyRatio, ActualFrequencyRatio);
                    if (Instrument.IsPlugged || reloadNeeded || !IsPlaying || stopping)
                    {
                        ReloadBuffer(FrequencyRatio, reloadNeeded);
                    }
                    CancelStop.Set();
                    //Set the frequency ratio to the voice
                    var realFrequencyRatio = Instrument.GetCorrectFrequencyRatio(FrequencyRatio);
                    Voice.SetFrequencyRatio(realFrequencyRatio);
                    ActualFrequencyRatio = FrequencyRatio;
                    PlayingPitch         = notePitch;
                }

                //Start the play if needed
                if (!IsPlaying)
                {
                    Voice.Start();
                    Voice.SetVolume(1, XAudio2.CommitNow);
                }

                //update state
                IsPlaying = true;
            });
        }
Example #3
0
        /// <summary>
        /// Play a sound from the sample file
        /// </summary>
        /// <param name="sampleIndex">sample index (starting from zero) in the file</param>
        public void PlaySound(float frequency, float pan = 1f, float volume = 1f)
        {
            var asy = Task.Run(() =>
            {
                if ((Voice == null) || (Instrument == null))
                {
                    return;
                }


                //Get the frequenc y of the pitch
                var FrequencyRatio = frequency / 440f;
                //See if we should use another sample
                var reloadNeeded = Instrument.ReloadNeeded(FrequencyRatio, ActualFrequencyRatio);
                if (Instrument.IsPlugged || reloadNeeded || !IsPlaying || stopping)
                {
                    ReloadBuffer(FrequencyRatio, reloadNeeded);
                }
                CancelStop.Set();
                //Set the frequency ratio to the voice
                var realFrequencyRatio = Instrument.GetCorrectFrequencyRatio(FrequencyRatio);
                Voice.SetFrequencyRatio(realFrequencyRatio);
                ActualFrequencyRatio = FrequencyRatio;
                PlayingPitch         = -1;

                float[] outputMatrix = new float[8];
                for (int i = 0; i < 8; i++)
                {
                    outputMatrix[i] = 0;
                }
                // pan of -1.0 indicates all left speaker,
                // 1.0 is all right speaker, 0.0 is split between left and right


                float left  = (0.5f - pan / 2) * volume;
                float right = (0.5f + pan / 2) * volume;


                switch (Master.ChannelMask)
                {
                case (int)Speakers.Mono:
                    outputMatrix[0] = 1.0f;
                    break;

                case (int)Speakers.Stereo:
                case (int)Speakers.TwoPointOne:
                case (int)Speakers.Surround:
                    outputMatrix[0] = left;
                    outputMatrix[1] = right;
                    break;

                case (int)Speakers.Quad:
                    outputMatrix[0] = outputMatrix[2] = left;
                    outputMatrix[1] = outputMatrix[3] = right;
                    break;

                case (int)Speakers.FourPointOne:
                    outputMatrix[0] = outputMatrix[3] = left;
                    outputMatrix[1] = outputMatrix[4] = right;
                    break;

                case (int)Speakers.FivePointOne:
                case (int)Speakers.SevenPointOne:
                case (int)Speakers.FivePointOneSurround:
                    outputMatrix[0] = outputMatrix[4] = left;
                    outputMatrix[1] = outputMatrix[5] = right;
                    break;

                case (int)Speakers.SevenPointOneSurround:
                    outputMatrix[0] = outputMatrix[4] = outputMatrix[6] = left;
                    outputMatrix[1] = outputMatrix[5] = outputMatrix[7] = right;
                    break;
                }
                Voice.SetOutputMatrix(null, Voice.VoiceDetails.InputChannelCount, Master.VoiceDetails.InputChannelCount, outputMatrix);


                //Start the play if needed
                if (!IsPlaying)
                {
                    Voice.Start();
                    Voice.SetVolume(1);
                }

                //update state
                IsPlaying = true;
            });
        }