Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 交换文件中的字节_C#_Audio - Fatal编程技术网

C# 交换文件中的字节

C# 交换文件中的字节,c#,audio,C#,Audio,我正在读取一个原始音频文件(CD曲目rip),进行字节交换,然后写回wav文件 我的例程正确地处理字节,但只处理大约一半的文件。我是一名VB.NET开发人员,不是真正的C#开发人员,并且此代码没有正确地转换为VB.NET(它会出现溢出错误) 与其说是“交换”,不如说是计算/修剪。 (短)((缓冲区[i+1]*256)+缓冲区[i]) 我相信它只写了一半的样本,但我不知道如何修复 public static short[] SwapBytesArray(string fileNam

我正在读取一个原始音频文件(CD曲目rip),进行字节交换,然后写回wav文件

我的例程正确地处理字节,但只处理大约一半的文件。我是一名VB.NET开发人员,不是真正的C#开发人员,并且此代码没有正确地转换为VB.NET(它会出现溢出错误)

与其说是“交换”,不如说是计算/修剪。 (短)((缓冲区[i+1]*256)+缓冲区[i])

我相信它只写了一半的样本,但我不知道如何修复

        public static short[] SwapBytesArray(string fileName)
    {
        byte[] buffer = System.IO.File.ReadAllBytes(fileName);
        long fileLength = buffer.LongLength;
        if ((fileLength & 1) == 1)
        {
            throw new ArgumentException("File length must be an even number of bytes");
        }
        System.Collections.Generic.List<short> sList = new System.Collections.Generic.List<short>();


        for (long i = 0; i < fileLength - 1; i += 2)
        {
            byte tmp = buffer[i];
            sList.Add((short)((buffer[i + 1] * 256) + buffer[i]));
            //buffer[i + 1] = tmp;
        }
        return sList.ToArray();
    }
publicstaticshort[]交换字节数组(字符串文件名)
{
byte[]buffer=System.IO.File.ReadAllBytes(文件名);
long fileLength=buffer.longlegth;
if((fileLength&1)==1)
{
抛出新ArgumentException(“文件长度必须为偶数字节”);
}
System.Collections.Generic.List sList=新的System.Collections.Generic.List();
for(长i=0;i
如果文件大小大于最大整数,则会溢出索引变量(index应该是long,因为num是long)。假设您使用buffer.LongLength而不是buffer.Length,因为已知数据较大,因此可能会出现问题。

如果文件大小大于最大整数,则会使索引变量溢出(索引应为long,因为num为long)。假设您使用buffer.LongLength而不是buffer.Length,因为已知数据较大,所以这可能是一个问题。

为什么要返回
short
的数组

如果要写回文件,
byte[]
数组不是更好的选择吗

i、 e

公共静态字节[]交换字节(字符串文件名)
{ 
byte[]buffer=System.IO.File.ReadAllBytes(文件名);
long fileLength=buffer.longlegth;
if((fileLength&1)==1)
{
抛出新ArgumentException(“文件长度必须为偶数字节”);
}
for(长i=0;i
为什么要返回
short
的数组

如果要写回文件,
byte[]
数组不是更好的选择吗

i、 e

公共静态字节[]交换字节(字符串文件名)
{ 
byte[]buffer=System.IO.File.ReadAllBytes(文件名);
long fileLength=buffer.longlegth;
if((fileLength&1)==1)
{
抛出新ArgumentException(“文件长度必须为偶数字节”);
}
for(长i=0;i
这似乎是对内存的极大浪费。您以字节序列的形式读入整个文件,然后将其复制到
short[]
数组中。您甚至可以在返回该数组时再次复制它


您的实际问题可能在于,当这个函数返回时(而不是字节),您写短路的方式。我想我们需要看看那段代码。

这似乎是对内存的极大浪费。您以字节序列的形式读入整个文件,然后将其复制到
short[]
数组中。您甚至可以在返回该数组时再次复制它


您的实际问题可能在于,当这个函数返回时(而不是字节),您写短路的方式。我认为我们需要看到这些代码。

我认为流媒体方法会更有效,更不容易出错。NET对流有很好的支持,因此类似于以下内容应该可以工作:

public void SwapBytesStreamed(Stream inputStream, Stream outputStream)
{
    byte[] bytePair = new byte[2];
    long offset = 0;
    while (offset < inputStream.Length)
    {
        int bytesRead = inputStream.Read(bytePair, 0, 2);
        if (bytesRead == 0) break;

        if (bytesRead == 2)
        {
            outputStream.WriteByte(bytePair[1]);
        }
        outputStream.WriteByte(bytePair[0]);

        offset += bytesRead;
    }
}

我认为流媒体方法更有效,更不容易出错。NET对流有很好的支持,因此类似于以下内容应该可以工作:

public void SwapBytesStreamed(Stream inputStream, Stream outputStream)
{
    byte[] bytePair = new byte[2];
    long offset = 0;
    while (offset < inputStream.Length)
    {
        int bytesRead = inputStream.Read(bytePair, 0, 2);
        if (bytesRead == 0) break;

        if (bytesRead == 2)
        {
            outputStream.WriteByte(bytePair[1]);
        }
        outputStream.WriteByte(bytePair[0]);

        offset += bytesRead;
    }
}

我添加了你的代码,并更改了上面的代码,但它仍然只处理了文件的一半。我添加了你的代码,并更改了上面的代码,但它仍然只处理了文件的一半。多亏了这群人的回答,我升级了上面的源代码,但它仍然只能处理一半的音频。多亏了这群人的回答,我升级了上面的源代码,但它仍然只能处理一半的音频。
public Stream SwapBytesInFile(string filename)
{
    Stream inputStream = File.Open(filename, FileMode.Open);
    MemoryStream outputStream = new MemoryStream();

    SwapBytesStreamed(inputStream, outputStream);
}