示例#1
0
        private byte[] RealMix(ReceivedRtp item1, ReceivedRtp item2)
        {
            if (item1 == null || item2 == null) return null;

            if (item1.size == 0 || item2.size == 0) return null;

            byte[] wavSrc1 = new byte[item1.size - headersize];
            byte[] wavSrc2 = new byte[item2.size - headersize];

            Array.Copy(item1.buff, headersize, wavSrc1, 0, (item1.size - headersize));
            Array.Copy(item2.buff, headersize, wavSrc2, 0, (item2.size - headersize));

            WaveMixerStream32 mixer = new WaveMixerStream32();
            // mixer.AutoStop = true;
            MemoryStream memstrem = new MemoryStream(wavSrc1);
            RawSourceWaveStream rawsrcstream = new RawSourceWaveStream(memstrem, this.codec);
            WaveFormatConversionStream conversionstream = new WaveFormatConversionStream(pcmFormat16, rawsrcstream);
            WaveChannel32 channelstream = new WaveChannel32(conversionstream);
            mixer.AddInputStream(channelstream);

            memstrem = new MemoryStream(wavSrc2);
            rawsrcstream = new RawSourceWaveStream(memstrem, this.codec);
            conversionstream = new WaveFormatConversionStream(pcmFormat16, rawsrcstream);
            channelstream = new WaveChannel32(conversionstream);
            mixer.AddInputStream(channelstream);
            mixer.Position = 0;

            Wave32To16Stream to16 = new Wave32To16Stream(mixer);
            var convStm = new WaveFormatConversionStream(pcmFormat8, to16);
            byte[] mixedbytes = new byte[(int)convStm.Length];
            int chk = convStm.Read(mixedbytes, 0, (int)convStm.Length);
            //Buffer.BlockCopy(tobyte, 0, writingBuffer, 0, tobyte.Length);

            memstrem.Close();
            rawsrcstream.Close();
            conversionstream.Close();
            channelstream.Close();

            convStm.Close(); convStm.Dispose(); convStm = null;
            to16.Close(); to16.Dispose(); to16 = null;
            mixer.Close(); mixer.Dispose(); mixer = null;

            return mixedbytes;
        }
示例#2
0
        private void ProcessMixing2(RcvData data, int dataSize)
        {
            string processingFn = string.Format("d:\\{0}_{1}_{2}.wav", data.seqnum, data.extension, data.peernumber);

            List<RecInfos> ls0 = lExtension0.FindAll(
                        delegate(RecInfos list)
                        {
                            return list.rcvData.Equals(data) && list.isExtension == 0;
                        });

            List<RecInfos> ls1 = lExtension1.FindAll(
                        delegate(RecInfos list)
                        {
                            return list.rcvData.Equals(data) && list.isExtension == 1;
                        });

            IsExtensionComparer isExtensionCompare = new IsExtensionComparer();
            ls0.Sort(isExtensionCompare);
            ls1.Sort(isExtensionCompare);

            int count = 0;
            int count0 = ls0.Count();
            int count1 = ls1.Count();

            if (count0 - count1 < 0)
                count = count0;
            else
                count = count1;

            byte[] buffWriting = new byte[320 * count];

            for (int i = 0; i < count; i++)
            {
                if (ls0[i].seq == ls1[i].seq)
                {
                    // 믹싱
                    // 코덱 종류에 따라서 바이트 길이는 달라질 수 있다. 실제로 만들 때 경우의 수 확인하고 만들어야 한다.
                    byte[] wavSrc0 = new byte[160];
                    byte[] wavSrc1 = new byte[160];

                    Array.Copy(ls0[i].voice, 12, wavSrc0, 0, wavSrc0.Length);
                    Array.Copy(ls1[i].voice, 12, wavSrc1, 0, wavSrc1.Length);

                    WaveMixerStream32 mixer = new WaveMixerStream32();
                    //mixer.AutoStop = true;

                    WaveChannel32 channelStm = null;

                    MemoryStream memStm = null;
                    BufferedStream bufStm = null;
                    RawSourceWaveStream rawSrcStm = null;
                    WaveFormatConversionStream conversionStm = null;

                    for (int j = 0; j < 2; j++)
                    {
                        if (j == 0)
                            memStm = new MemoryStream(wavSrc0);
                        else
                            memStm = new MemoryStream(wavSrc1);

                        bufStm = new BufferedStream(memStm);
                        rawSrcStm = new RawSourceWaveStream(bufStm, mulawFormat);
                        conversionStm = new WaveFormatConversionStream(pcmFormat, rawSrcStm);

                        channelStm = new WaveChannel32(conversionStm);
                        mixer.AddInputStream(channelStm);
                    }
                    mixer.Position = 0;

                    Wave32To16Stream to16 = new Wave32To16Stream(mixer);
                    var convStm = new WaveFormatConversionStream(pcmFormat, to16);
                    byte[] tobyte = new byte[(int)convStm.Length];
                    int chk = convStm.Read(tobyte, 0, (int)convStm.Length);
                    Buffer.BlockCopy(tobyte, 0, buffWriting, i * tobyte.Length, tobyte.Length);

                    conversionStm.Close();
                    rawSrcStm.Close();
                    bufStm.Close();
                    memStm.Close();

                    convStm.Close();
                    to16.Close();
                    channelStm.Close();
                    mixer.Close();

                    // 삭제
                    lExtension0.Remove(ls0[i]);
                    lExtension1.Remove(ls1[i]);
                }
                else if (ls0[i].seq - ls1[i].seq < 0)
                {
                    // ls0 만 믹싱
                    // ls0 원본에 byte[] 붙임 > 원본 byte[]를 wavesream 으로 변환 > wave 파일로 저장

                    // 믹싱
                    // 코덱 종류에 따라서 바이트 길이는 달라질 수 있다. 실제로 만들 때 경우의 수 확인하고 만들어야 한다.
                    byte[] wavSrc0 = new byte[160];
                    byte[] wavSrc1 = new byte[160];

                    Array.Copy(ls0[i].voice, 12, wavSrc0, 0, wavSrc0.Length);
                    Array.Copy(ls1[i].voice, 12, wavSrc1, 0, wavSrc1.Length);

                    WaveMixerStream32 mixer = new WaveMixerStream32();
                    //mixer.AutoStop = true;

                    WaveChannel32 channelStm = null;

                    MemoryStream memStm = null;
                    BufferedStream bufStm = null;
                    RawSourceWaveStream rawSrcStm = null;
                    WaveFormatConversionStream conversionStm = null;

                    for (int j = 0; j < 2; j++)
                    {
                        if (j == 0)
                            memStm = new MemoryStream(wavSrc0);
                        else
                            memStm = new MemoryStream(wavSrc1);

                        bufStm = new BufferedStream(memStm);
                        rawSrcStm = new RawSourceWaveStream(bufStm, mulawFormat);
                        conversionStm = new WaveFormatConversionStream(pcmFormat, rawSrcStm);

                        channelStm = new WaveChannel32(conversionStm);
                        mixer.AddInputStream(channelStm);
                    }
                    mixer.Position = 0;

                    Wave32To16Stream to16 = new Wave32To16Stream(mixer);
                    var convStm = new WaveFormatConversionStream(pcmFormat, to16);
                    byte[] tobyte = new byte[(int)convStm.Length];
                    int chk = convStm.Read(tobyte, 0, (int)convStm.Length);
                    Buffer.BlockCopy(tobyte, 0, buffWriting, i * tobyte.Length, tobyte.Length);

                    conversionStm.Close();
                    rawSrcStm.Close();
                    bufStm.Close();
                    memStm.Close();

                    convStm.Close();
                    to16.Close();
                    channelStm.Close();
                    mixer.Close();

                    // 삭제
                    lExtension0.Remove(ls0[i]);
                    ls1.Insert(i + 1, ls1[i]);
                }
                else if (ls0[i].seq - ls1[i].seq > 0)
                {
                    // ls1 만 믹싱
                    // ls1 원본에 byte[] 붙임 > 원본 byte[]를 wavesream 으로 변환 > wave 파일로 저장

                    // 믹싱
                    // 코덱 종류에 따라서 바이트 길이는 달라질 수 있다. 실제로 만들 때 경우의 수 확인하고 만들어야 한다.
                    byte[] wavSrc0 = new byte[160];
                    byte[] wavSrc1 = new byte[160];

                    Array.Copy(ls0[i].voice, 12, wavSrc0, 0, wavSrc0.Length);
                    Array.Copy(ls1[i].voice, 12, wavSrc1, 0, wavSrc1.Length);

                    WaveMixerStream32 mixer = new WaveMixerStream32();
                    //mixer.AutoStop = true;

                    WaveChannel32 channelStm = null;

                    MemoryStream memStm = null;
                    BufferedStream bufStm = null;
                    RawSourceWaveStream rawSrcStm = null;
                    WaveFormatConversionStream conversionStm = null;

                    for (int j = 0; j < 2; j++)
                    {
                        if (j == 0)
                            memStm = new MemoryStream(wavSrc0);
                        else
                            memStm = new MemoryStream(wavSrc1);

                        bufStm = new BufferedStream(memStm);
                        rawSrcStm = new RawSourceWaveStream(bufStm, mulawFormat);
                        conversionStm = new WaveFormatConversionStream(pcmFormat, rawSrcStm);

                        channelStm = new WaveChannel32(conversionStm);
                        mixer.AddInputStream(channelStm);
                    }
                    mixer.Position = 0;

                    Wave32To16Stream to16 = new Wave32To16Stream(mixer);
                    var convStm = new WaveFormatConversionStream(pcmFormat, to16);
                    byte[] tobyte = new byte[(int)convStm.Length];
                    int chk = convStm.Read(tobyte, 0, (int)convStm.Length);
                    Buffer.BlockCopy(tobyte, 0, buffWriting, i * tobyte.Length, tobyte.Length);

                    conversionStm.Close();
                    rawSrcStm.Close();
                    bufStm.Close();
                    memStm.Close();

                    convStm.Close();
                    to16.Close();
                    channelStm.Close();
                    mixer.Close();

                    // 삭제
                    lExtension1.Remove(ls1[i]);
                    ls0.Insert(i + 1, ls0[i]);
                }
            }

            // 10개의 버프를 바이트로 만들어 WaveFileWrite
            WaveFileWriting(buffWriting, processingFn);
        }