tags:

views:

329

answers:

2

Hi i got this code and i got 2 errors which i can't get rid of.

Any help pls?

namespace pleasework
{
    public partial class Form1 : Form
    {
        private Thread _echoThread;
        private Capture _captureDevice;
        private CaptureBuffer _captureBuffer;
        private Device _playbackDevice;
        private SecondaryBuffer _playbackBuffer;
        private int _bufferSize;
        private const int _bufferPositions = 4;
        private AutoResetEvent _notificationEvent = null;
        private BufferPositionNotify[] _positionNotify = new BufferPositionNotify[_bufferPositions + 1];
        private Notify _echoNotify = null;

        private void EchoThread()
        {
            int offset = 0;



            _captureBuffer.Start(true);
            _playbackBuffer.Play(0, BufferPlayFlags.Looping);
            //byte[] buffer = (byte[])_captureBuffer.Read(offset, typeof(byte), LockFlag.FromWriteCursor, _bufferSize);


            for (; ; )
            {
                _notificationEvent.WaitOne(Timeout.Infinite, true);

                byte[] buffer = (byte[])_captureBuffer.Read(offset, typeof(byte), LockFlag.None, _bufferSize);

                _playbackBuffer.Write(offset, buffer, LockFlag.None);
                offset = (offset + _bufferSize) % (_bufferPositions * _bufferSize);
            }

        }

        public Form1()
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();

            _captureDevice = new Capture();

            short channels = 2;


            short bitsPerSample = 16;


            int samplesPerSecond = 22050;

            //Set up the wave format to be captured
            WaveFormat waveFormat = new WaveFormat();
            waveFormat.Channels = channels;
            waveFormat.FormatTag = WaveFormatTag.Pcm;
            waveFormat.SamplesPerSecond = samplesPerSecond;
            waveFormat.BitsPerSample = bitsPerSample;
            waveFormat.BlockAlign = (short)(channels * (bitsPerSample / 8));
            waveFormat.AverageBytesPerSecond = waveFormat.BlockAlign * samplesPerSecond;

            _bufferSize = waveFormat.AverageBytesPerSecond / 20;

            CaptureBufferDescription captureBufferDescription = new CaptureBufferDescription();
            captureBufferDescription.BufferBytes = _bufferPositions * _bufferSize;
            captureBufferDescription.Format = waveFormat;
            _captureBuffer = new CaptureBuffer(captureBufferDescription, _captureDevice);

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

            BufferDescription playbackBufferDescription = new BufferDescription();
            playbackBufferDescription.BufferBytes = _bufferPositions * _bufferSize;
            playbackBufferDescription.Format = waveFormat;
            _playbackBuffer = new SecondaryBuffer(playbackBufferDescription, _playbackDevice);

            _echoThread = new Thread(new ThreadStart(EchoThread));
            _echoThread.Start();


            _notificationEvent = new AutoResetEvent(false);
            for (int i = 0; i < _bufferPositions; i++)
            {

                _positionNotify.Offset = (_bufferSize * i) + _bufferSize - 1; // got error here
                _positionNotify.EventNotifyHandle = _notificationEvent.SafeWaitHandle.DangerousGetHandle();// got error here
            }
            _echoNotify = new Notify(_captureBuffer);
            _echoNotify.SetNotificationPositions(_positionNotify, _bufferPositions);

        }

        private void EchoClose(object sender, FormClosingEventArgs e)
        {
            _echoThread.Abort();
        }
    }
}

Thanks!

+1  A: 

You are setting _positionNotify to an array with _bufferPosition + 1 elements. Yet, when you are in your for loop you never specify which of the elements you are setting the Offset and EventNotifyHandle for. Also I think you need to add one additional line so actually create a new instance of the BufferPositionNotify structure. Try changing those lines to something like this:

        for (int i = 0; i < _bufferPositions; i++)
        {
            _positionNotify[i] = new BufferPositionNotify();
            _positionNotify[i].Offset = (_bufferSize * i) + _bufferSize - 1;
            _positionNotify[i].EventNotifyHandle = 
                _notificationEvent.SafeWaitHandle.DangerousGetHandle();
        }
Tim C
your answer eliminated the error but it seems the program is not functioning :S is their any chance i can send you my whole program?thanks
Tristan Demanuele
It worked thanks alot mate!!! you save my ass
Tristan Demanuele
so i now the code is working by now i got a handled exception that i can't get rid of. any help?thanks
Tristan Demanuele
A: 

I tried the code. First it didn't work. Instead of: private BufferPositionNotify[] _positionNotify = new BufferPositionNotify[_bufferPositions + 1]; it should be private BufferPositionNotify[] _positionNotify = new BufferPositionNotify[_bufferPositions];. Otherwhise you have an element to much in your array.

mc