C# 内存复制和数组复制示例

内存复制-Marshal.Copy:

 // 复制当前传入的数据
 byte[] pcm_src = new byte[len];
 // 复制数据到二进制数组
 Marshal.Copy(pcm, pcm_src, 0, len);

数组复制-Array.Copy:

// 复制当前传入的数据
byte[] pcm_src = new byte[len];
// 复制数据到二进制数组
Marshal.Copy(pcm, pcm_src, 0, len);
// 复制新传入的数据到新的数组结构
Array.Copy(pcm_src, 0, pcm_dest, bts_left.Length, len)

测试示例:

        // 采样数
        ushort samples = 320;

        // 上次剩余的字节
        byte[] bts_left;

        class aa
        {
            public byte[] pcm;
            public int len;
        }

        private List<aa> data = new List<aa>();
    
        public void PlayAudio(IntPtr pcm, int len)
        {
            lock (this)
            {
                int samples_len = samples * 2;

                if (len == samples_len)
                {
                    byte[] bts = new byte[len];
                    Marshal.Copy(pcm, bts, 0, len);
                    data.Add(new aa
                    {
                        len = len,
                        pcm = bts
                    });
                }
                else
                // 此处解决采样数为1024时分割可以复制数据的大小
                {
                    var len_temp = bts_left ==null? len : len + bts_left.Length;

                    byte[] pcm_dest = new byte[len_temp];
                    byte[] bts = new byte[samples_len];
                    // 复制数据
                    if (bts_left != null)
                    {
                        // 复制长度不够的数据
                        Array.Copy(bts_left,0, pcm_dest, 0, bts_left.Length);
                        // 复制当前传入的数据
                        byte[] pcm_src = new byte[len];
                        // 复制数据到二进制数组
                        Marshal.Copy(pcm, pcm_src, 0, len);
                        // 复制新传入的数据到新的数组结构
                        Array.Copy(pcm_src, 0, pcm_dest, bts_left.Length, len);
                       
                        bts_left = null;
                    }
                    else
                    {
                        Marshal.Copy(pcm, pcm_dest, 0, len_temp);
                    }
                    // 获取播放数据
                    int index = 0,count= len_temp / samples_len;
                    int startIndex=0;
                    Console.WriteLine(">>>total:"+ len_temp + ",times:" + count + ",left:" + len_temp % samples_len);
                    while (index< count)
                    {
                        // 复制满足长度的数据
                        Array.Copy(pcm_dest, startIndex, bts, 0, samples_len);
                        // 添加数据
                        data.Add(new aa
                        {
                            len = samples_len,
                            pcm = bts
                        });
                        index++;
                        // 计算下一次的开始位置
                        startIndex = index * samples_len; 
                    }
                    // 暂存长度不够的数据
                    var left_len = len_temp % samples_len;
                    if (left_len > 0)
                    {
                        bts_left = new byte[left_len];
                        // 复制不够长度的数据
                        Array.Copy(pcm_dest, startIndex, bts_left, 0, left_len);
                    }
                }
            }

            //SDL.SDL_Delay(10);
        }

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值