1

rawData1 配列にすべてのデータがありますが、これは短い配列です。バイト配列に変換しました。次に、wav ファイル ヘッダーを作成し、そのヘッダーとバイト配列データをファイルに書き込みます。しかし、「ファイルの再生中に Windows メディア プレーヤーに問題が発生しました」というエラーが表示されます。

        public void save file
        {
         byte[] byte_array = new byte[rawData1.Length * 2];
        for (int i = 0; i < rawData1.Length; ++i)
        {
            //byte_array[2 * i] = getByte1(rawData1[i]);
            //byte_array[2 * i + 1] = getByte2(rawData1[i]);
            byte_array[2 * i] = getByte2(rawData1[i]);
            byte_array[2 * i + 1] = getByte1(rawData1[i]);
        }

        uint numsamples = 44100;
        ushort numchannels = 2;
        ushort samplelength = 1; // in bytes
        uint samplerate = 22050;

        StorageFolder storageFolder = KnownFolders.MusicLibrary;
        StorageFile file = await storageFolder.CreateFileAsync("sample.wav",     CreationCollisionOption.GenerateUniqueName);

        Stream stream = await file.OpenStreamForWriteAsync();
        BinaryWriter wr = new BinaryWriter(stream);

        wr.Write("RIFF".ToCharArray());
        wr.Write(36 + numsamples * numchannels * samplelength);
        wr.Write("WAVEfmt".ToCharArray());
        wr.Write(16);
        wr.Write((ushort)1);
        wr.Write(numchannels);
        wr.Write(samplerate);
        wr.Write(samplerate * samplelength * numchannels);
        wr.Write(samplelength * numchannels);
        wr.Write((ushort)(8 * samplelength));
        wr.Write("data".ToCharArray());
        wr.Write(numsamples * samplelength);
        wr.Write(byte_array);
        wr.Dispose();
        stream.Dispose();
       }
       public static byte getByte1(short s)
       {
        return (byte)s;
       }

       public static byte getByte2(short s)
       {
        int temp= s >> 8;
        return (byte)temp;
       }

誰でも私を助けることができますか?

4

1 に答える 1

2

ここでは、WAV 形式の非常に詳細な説明を示します。

The canonical WAVE format starts with the RIFF header:
-------------------------------------------------
Offset  Size  Name             Description
-------------------------------------------------
0         4   ChunkID          Contains the letters "RIFF" in ASCII form
                               (0x52494646 big-endian form).
4         4   ChunkSize        36 + SubChunk2Size, or more precisely:
                               4 + (8 + SubChunk1Size) + (8 + SubChunk2Size)
                               This is the size of the rest of the chunk 
                               following this number.  This is the size of the 
                               entire file in bytes minus 8 bytes for the
                               two fields not included in this count:
                               ChunkID and ChunkSize.
8         4   Format           Contains the letters "WAVE"
                               (0x57415645 big-endian form).

The "WAVE" format consists of two subchunks: "fmt " and "data":
The "fmt " subchunk describes the sound data's format:

12        4   Subchunk1ID      Contains the letters "fmt "
                               (0x666d7420 big-endian form).
16        4   Subchunk1Size    16 for PCM.  This is the size of the
                               rest of the Subchunk which follows this number.
20        2   AudioFormat      PCM = 1 (i.e. Linear quantization)
                               Values other than 1 indicate some 
                               form of compression.
22        2   NumChannels      Mono = 1, Stereo = 2, etc.
24        4   SampleRate       8000, 44100, etc.
28        4   ByteRate         == SampleRate * NumChannels * BitsPerSample/8
32        2   BlockAlign       == NumChannels * BitsPerSample/8
                               The number of bytes for one sample including
                               all channels. I wonder what happens when
                               this number isn't an integer?
34        2   BitsPerSample    8 bits = 8, 16 bits = 16, etc.
          2   ExtraParamSize   if PCM, then doesn't exist
          X   ExtraParams      space for extra parameters

The "data" subchunk contains the size of the data and the actual sound:

36        4   Subchunk2ID      Contains the letters "data"
                               (0x64617461 big-endian form).
40        4   Subchunk2Size    == NumSamples * NumChannels * BitsPerSample/8
                               This is the number of bytes in the data.
                               You can also think of this as the size
                               of the read of the subchunk following this 
                               number.
44        *   Data             The actual sound data.

詳細はこちら

https://ccrma.stanford.edu/courses/422/projects/WaveFormat/

于 2013-08-24T13:16:19.637 に答える