Ejemplo n.º 1
0
        private bool UploadSound(byte[] soundBuffer)
        {
            int chunkHeaderLen = 0;

            byte[] chunkHeader = Zd2911Utils.CreateChunkHeader(soundBuffer, dataChunk);
            int    len         = BitConverter.ToInt32(chunkHeader, 4);
            int    downSize    = len + 4;

            byte[] downData = new byte[downSize];
            Array.Copy(chunkHeader, 4, downData, 0, 4);
            Array.Copy(soundBuffer, dataChunk + chunkHeaderLen, downData, 4, len);

            int    unit = 1024 * 60, i;
            bool   result        = false;
            int    n             = (int)downSize / unit;
            object extraProperty = new object();
            object extraData     = new object();

            for (i = 0; i < n; i++)
            {
                byte[] dataBytes = new byte[unit];
                Array.Copy(downData, i * unit, dataBytes, 0, unit);
                List <int> soundParam = new List <int>();
                soundParam.Add(cbo_SoundType.SelectedIndex + 8);
                soundParam.Add(i * unit);
                extraProperty = soundParam;
                extraData     = dataBytes;
                result        = deviceConnection.SetProperty(DeviceProperty.UploadSound, extraProperty, device, extraData);
                if (false == result)
                {
                    return(false);
                }
            }

            n = downSize % unit;
            if (n > 0)
            {
                byte[] dataBytes = new byte[n];
                Array.Copy(downData, i * unit, dataBytes, 0, n);
                List <int> soundParam = new List <int>();
                soundParam.Add(cbo_SoundType.SelectedIndex + 8);
                soundParam.Add(i * unit);
                extraProperty = soundParam;
                extraData     = dataBytes;
                result        = deviceConnection.SetProperty(DeviceProperty.UploadSound, extraProperty, device, extraData);
                if (false == result)
                {
                    return(false);
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
        private bool CheckWaveFormat(byte[] buffer, ref int offSet)
        {
            offSet = 0;
            string riff = Encoding.ASCII.GetString(buffer, 0, 4);
            string wave = Encoding.ASCII.GetString(buffer, 8, 4);
            string fmt  = Encoding.ASCII.GetString(buffer, 12, 4);

            if (false == riff.Equals("RIFF") || false == wave.Equals("WAVE") || false == fmt.Equals("fmt "))
            {
                MessageBox.Show("Incorrect Wave Format", "Pormpt", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return(false);
            }

            int waveFormatSize = BitConverter.ToInt32(buffer, 16);

            byte[] waveBytes = new byte[18];
            Array.Copy(buffer, 20, waveBytes, 0, waveBytes.Length);

            UInt16 formatTag = BitConverter.ToUInt16(waveBytes, 0);

            if (1 != formatTag)
            {
                MessageBox.Show("Audio Format is not PCM", "Pormpt", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return(false);
            }

            UInt16 channels = BitConverter.ToUInt16(waveBytes, 2);

            if (1 != channels)
            {
                MessageBox.Show("Channels is not Stereo", "Pormpt", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return(false);
            }

            UInt32 samplesPerSec = BitConverter.ToUInt32(waveBytes, 4);

            if (22050 != samplesPerSec)
            {
                MessageBox.Show("Audio sample rate is not 22.05KHZ", "Pormpt", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return(false);
            }

            UInt16 bitsPerSample = BitConverter.ToUInt16(waveBytes, 14);

            if (16 != bitsPerSample)
            {
                MessageBox.Show("Audio sample size is not 8bit", "Pormpt", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return(false);
            }

            int chunkHeaderLen = 0;
            int tmpOffSet      = 20 + waveFormatSize;

            byte[] chunkHeader = Zd2911Utils.CreateChunkHeader(buffer, tmpOffSet);
            string fact        = Encoding.ASCII.GetString(chunkHeader, 0, 4);
            int    len         = BitConverter.ToInt32(chunkHeader, 4);

            if (fact.Equals("fact"))
            {
                offSet = offSet + chunkHeaderLen + len;
            }
            offSet = tmpOffSet;

            chunkHeader = Zd2911Utils.CreateChunkHeader(buffer, offSet);
            string data = Encoding.ASCII.GetString(chunkHeader, 0, 4);

            len = BitConverter.ToInt32(chunkHeader, 4);
            if (false == data.Equals("data"))
            {
                MessageBox.Show("Incorrect Wave Format", "Pormpt", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return(false);
            }

            if (1024 * 75 < len)
            {
                MessageBox.Show("Data size is too big. Must be less than 75K byte", "Pormpt", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return(false);
            }

            return(true);
        }