Пример #1
0
        public SoundManager(System.IntPtr handle, short BitsPerSample, short Channels, int SamplesPerSecond)
        {
            System.AppDomain.CurrentDomain.AssemblyResolve += new System.ResolveEventHandler(CurrentDomain_AssemblyResolve);
            SlimDX.Multimedia.WaveFormat format = new SlimDX.Multimedia.WaveFormat();
            format.BitsPerSample         = BitsPerSample;
            format.Channels              = Channels;
            format.SamplesPerSecond      = SamplesPerSecond;
            format.BlockAlignment        = (short)(format.Channels * format.BitsPerSample / 8);
            format.AverageBytesPerSecond = format.SamplesPerSecond * format.BlockAlignment;
            //format.FormatTag = WaveFormatTag.Pcm;
            format.FormatTag = SlimDX.Multimedia.WaveFormatTag.Pcm;

            device = new XAudio2(XAudio2Flags.None, ProcessorSpecifier.AnyProcessor);

            device.StartEngine();

            masteringVoice = new MasteringVoice(device, Channels, SamplesPerSecond);

            sourceVoice = new SourceVoice(device, format, VoiceFlags.None);

            //FilterParameters fp = new FilterParameters();
            //fp.Frequency = 0.5f;//sourceVoice.FilterParameters.Frequency;
            //fp.OneOverQ = 0.5f;//sourceVoice.FilterParameters.OneOverQ;
            //fp.Type = FilterType.LowPassFilter;
            //sourceVoice.FilterParameters = fp;

            //sourceVoice.BufferEnd += new System.EventHandler<ContextEventArgs>(sourceVoice_BufferEnd);
            // sourceVoice.StreamEnd += new System.EventHandler(sourceVoice_StreamEnd);
            // sourceVoice.BufferStart += new System.EventHandler<ContextEventArgs>(sourceVoice_BufferStart);
            // sourceVoice.VoiceError += new EventHandler<ErrorEventArgs>(sourceVoice_VoiceError);

            sourceVoice.Volume = 0.5f;
            buffer             = new AudioBuffer();
            buffer.AudioData   = new System.IO.MemoryStream();

            waveFormat     = format;
            bytesPerSample = (waveFormat.BitsPerSample / 8) * Channels;
            for (int i = 0; i < BUFFER_COUNT; i++)
            {
                //sampleData[i] = new float[SAMPLE_SIZE * Channels];
                sampleData[i] = new short[SAMPLE_SIZE * Channels];
                bData[i]      = new byte[SAMPLE_SIZE * bytesPerSample];
            }
            sourceVoice.SubmitSourceBuffer(buffer);
        }
Пример #2
0
        private void Initialize()
        {
            if (this.initialized)
            {
                return;
            }

            this.initialized = true;

            this.directSound = new DirectSound(DirectSoundGuid.DefaultPlaybackDevice);
            this.directSound.SetCooperativeLevel(FourDO.Program.GetMainWindowHwnd(), CooperativeLevel.Normal);

            this.bufferFormat                       = new SlimDX.Multimedia.WaveFormat();
            this.bufferFormat.Channels              = 2;
            this.bufferFormat.BitsPerSample         = 16;
            this.bufferFormat.FormatTag             = SlimDX.Multimedia.WaveFormatTag.Pcm;
            this.bufferFormat.SamplesPerSecond      = 44100;
            this.bufferFormat.BlockAlignment        = (short)(bufferFormat.Channels * (bufferFormat.BitsPerSample / 8));
            this.bufferFormat.AverageBytesPerSecond = bufferFormat.SamplesPerSecond * bufferFormat.BlockAlignment;

            this.bufferDescription             = new SoundBufferDescription();
            this.bufferDescription.Flags       = BufferFlags.ControlVolume | BufferFlags.GlobalFocus;
            this.bufferDescription.Format      = this.bufferFormat;
            this.bufferDescription.SizeInBytes = this.bufferFormat.AverageBytesPerSecond * 2;             // Two seconds of buffer

            if (bufferDescription.SizeInBytes % TEMP_BUFFER_SIZE != 0 || 44100 % TEMP_BUFFER_SIZE != 0)
            {
                throw new Exception("Audio buffer size needs to be a multiple of the temporary buffer size");
            }

            this.playBuffer = new SecondarySoundBuffer(directSound, this.bufferDescription);

            this.emptyBuffer = new uint[this.bufferDescription.SizeInBytes / sizeof(uint)];

            this.play_copy_guess_offset  = (int)(this.bufferFormat.SamplesPerSecond * (PLAY_COPY_GUESS_MILLISECONDS / (double)1000));
            this.play_copy_guess_offset *= this.bufferFormat.BlockAlignment;

            this.RecalculateBufferOffsets(this.bufferDelayMilliseconds);

            this.offTimeTicksThreshhold = (long)((TIME_OFF_DURATION_MILLISECONDS / ((double)1000) * PerformanceCounter.Frequency));
        }