Esempio n. 1
0
        public SoundPlayer(Control owner, PullAudio pullAudio, short channels)
        {
            _channels  = channels;
            _pullAudio = pullAudio;

            _soundDevice = new Device();
            _soundDevice.SetCooperativeLevel(owner, CooperativeLevel.Priority);

            // Set up our wave format to 44,100Hz, with 16 bit resolution
            var wf = new WaveFormat
            {
                FormatTag = WaveFormatTag.Pcm, SamplesPerSecond = 44100, BitsPerSample = 16, Channels = channels
            };

            wf.BlockAlign            = (short)(wf.Channels * wf.BitsPerSample / 8);
            wf.AverageBytesPerSecond = wf.SamplesPerSecond * wf.BlockAlign;

            _samplesPerUpdate = 512;

            // Create a buffer with 2 seconds of sample data
            var bufferDesc = new BufferDescription(wf)
            {
                BufferBytes           = _samplesPerUpdate * wf.BlockAlign * 2,
                ControlPositionNotify = true,
                GlobalFocus           = true
            };

            _soundBuffer = new SecondaryBuffer(bufferDesc, _soundDevice);

            var notify = new Notify(_soundBuffer);

            _fillEvent[0] = new AutoResetEvent(false);
            _fillEvent[1] = new AutoResetEvent(false);

            // Set up two notification events, one at halfway, and one at the end of the buffer
            var posNotify = new BufferPositionNotify[2];

            posNotify[0] = new BufferPositionNotify
            {
                Offset            = bufferDesc.BufferBytes / 2 - 1,
                EventNotifyHandle = _fillEvent[0].SafeWaitHandle.DangerousGetHandle()
            };
            posNotify[1] = new BufferPositionNotify
            {
                Offset            = bufferDesc.BufferBytes - 1,
                EventNotifyHandle = _fillEvent[1].SafeWaitHandle.DangerousGetHandle()
            };

            notify.SetNotificationPositions(posNotify);

            _thread = new Thread(SoundPlayback)
            {
                Priority = ThreadPriority.Highest
            };

            Pause();
            _running = true;

            _thread.Start();
        }
Esempio n. 2
0
        public SoundPlayer(Control owner, PullAudio pullAudio, string sample, short channels)
        {
            if (sample == null || File.Exists(sample) == false)
                return;
            this.channels = channels;
            this.pullAudio = pullAudio;
            this.samplefile = sample;
            this._owner = owner;

            this.soundDevice = new Device();
            this.soundDevice.SetCooperativeLevel(_owner, CooperativeLevel.Priority);

            // Set up our wave format to 44,100Hz, with 16 bit resolution
            WaveFormat wf = new WaveFormat();
            wf.FormatTag = WaveFormatTag.Pcm;
            wf.SamplesPerSecond = 44100;
            wf.BitsPerSample = 16;
            wf.Channels = channels;
            wf.BlockAlign = (short)(wf.Channels * wf.BitsPerSample / 8);
            wf.AverageBytesPerSecond = wf.SamplesPerSecond * wf.BlockAlign;

            this.samplesPerUpdate = 512;

            // Create a buffer with 2 seconds of sample data
            BufferDescription bufferDesc = new BufferDescription();
            bufferDesc.BufferBytes = this.samplesPerUpdate * wf.BlockAlign * 2;
            bufferDesc.ControlPositionNotify = true;
            bufferDesc.GlobalFocus = true;
            bufferDesc.ControlFrequency = true;
            bufferDesc.ControlEffects = true;
            bufferDesc.ControlVolume = true;

            this.soundBuffer = new SecondaryBuffer(samplefile, bufferDesc, this.soundDevice);
            this.soundBuffer.Volume = 0;

            Notify notify = new Notify(this.soundBuffer);
            fillEvent[0] = new AutoResetEvent(false);
            fillEvent[1] = new AutoResetEvent(false);

            // Set up two notification events, one at halfway, and one at the end of the buffer
            BufferPositionNotify[] posNotify = new BufferPositionNotify[2];
            posNotify[0] = new BufferPositionNotify();
            posNotify[0].Offset = bufferDesc.BufferBytes / 2 - 1;
            posNotify[0].EventNotifyHandle = fillEvent[0].Handle;
            posNotify[1] = new BufferPositionNotify();
            posNotify[1].Offset = bufferDesc.BufferBytes - 1;
            posNotify[1].EventNotifyHandle = fillEvent[1].Handle;

            notify.SetNotificationPositions(posNotify);

            this.thread = new Thread(new ThreadStart(SoundPlayback));
            this.thread.Priority = ThreadPriority.Lowest;
            this.thread.IsBackground = true;

            this.Pause();
            this.running = true;

            this.thread.Start();
        }
        public SoundPlayer(Control owner, PullAudio pullAudio, short channels)
        {
            this.channels  = channels;
            this.pullAudio = pullAudio;

            this.soundDevice = new Device();
            this.soundDevice.SetCooperativeLevel(owner, CooperativeLevel.Priority);

            // Set up our wave format to 44,100Hz, with 16 bit resolution
            WaveFormat wf = new WaveFormat();

            wf.FormatTag             = WaveFormatTag.Pcm;
            wf.SamplesPerSecond      = 44100;
            wf.BitsPerSample         = 16;
            wf.Channels              = channels;
            wf.BlockAlign            = (short)(wf.Channels * wf.BitsPerSample / 8);
            wf.AverageBytesPerSecond = wf.SamplesPerSecond * wf.BlockAlign;

            this.samplesPerUpdate = 512;

            // Create a buffer with 2 seconds of sample data
            BufferDescription bufferDesc = new BufferDescription(wf);

            bufferDesc.BufferBytes           = this.samplesPerUpdate * wf.BlockAlign * 2;
            bufferDesc.ControlPositionNotify = true;
            bufferDesc.GlobalFocus           = true;

            this.soundBuffer = new SecondaryBuffer(bufferDesc, this.soundDevice);

            Notify notify = new Notify(this.soundBuffer);

            fillEvent[0] = new AutoResetEvent(false);
            fillEvent[1] = new AutoResetEvent(false);

            // Set up two notification events, one at halfway, and one at the end of the buffer
            BufferPositionNotify[] posNotify = new BufferPositionNotify[2];
            posNotify[0]                   = new BufferPositionNotify();
            posNotify[0].Offset            = bufferDesc.BufferBytes / 2 - 1;
            posNotify[0].EventNotifyHandle = fillEvent[0].Handle;
            posNotify[1]                   = new BufferPositionNotify();
            posNotify[1].Offset            = bufferDesc.BufferBytes - 1;
            posNotify[1].EventNotifyHandle = fillEvent[1].Handle;

            notify.SetNotificationPositions(posNotify);

            this.thread          = new Thread(new ThreadStart(SoundPlayback));
            this.thread.Priority = ThreadPriority.Highest;

            this.Pause();
            this.running = true;

            this.thread.Start();
        }
        public DSoundPlayer(Control owner, PullAudio pullAudio, Qaryan.Audio.WaveFormat format)
        {
            this.pullAudio = pullAudio;

            this.soundDevice = new Device();
            this.soundDevice.SetCooperativeLevel(owner, CooperativeLevel.Normal);

            // Set up our wave format to 44,100Hz, with 16 bit resolution
            DirectSound.WaveFormat wf = ConvertWaveFormat(format);
//            wf.FormatTag = DirectSound.WaveFormatTag.Pcm;
//            wf.SamplesPerSecond = 44100;
//            wf.BitsPerSample = 16;
//            wf.Channels = channels;
//            wf.BlockAlign = (short)(wf.Channels * wf.BitsPerSample / 8);
//            wf.AverageBytesPerSecond = wf.SamplesPerSecond * wf.BlockAlign;

            this.samplesPerUpdate = 1024;

            // Create a buffer with 5 seconds of sample data
            bufferDesc                       = new BufferDescription(wf);
            bufferDesc.BufferBytes           = this.samplesPerUpdate * wf.BlockAlign * 5;
            bufferDesc.ControlPositionNotify = true;
            bufferDesc.GlobalFocus           = true;

            this.soundBuffer = new SecondaryBuffer(bufferDesc, this.soundDevice);

            Notify notify = new Notify(this.soundBuffer);

            fillEvent[0] = new AutoResetEvent(false);
            fillEvent[1] = new AutoResetEvent(false);

            // Set up two notification events, one at halfway, and one at the end of the buffer
            BufferPositionNotify[] posNotify = new BufferPositionNotify[2];
            posNotify[0]                   = new BufferPositionNotify();
            posNotify[0].Offset            = bufferDesc.BufferBytes / 2 - 1;
            posNotify[0].EventNotifyHandle = fillEvent[0].SafeWaitHandle.DangerousGetHandle();
            posNotify[1]                   = new BufferPositionNotify();
            posNotify[1].Offset            = bufferDesc.BufferBytes - 1;
            posNotify[1].EventNotifyHandle = fillEvent[1].SafeWaitHandle.DangerousGetHandle();

            notify.SetNotificationPositions(posNotify);

            this.thread = new Thread(new ThreadStart(SoundPlayback));
            this.thread.SetApartmentState(ApartmentState.STA);
            this.thread.Name     = "SoundPlayback";
            this.thread.Priority = ThreadPriority.Highest;

            this.Pause();
            this.running = true;

            this.thread.Start();
        }