コード例 #1
0
            public WFOTransporter(WaveFileObject obj)
            {
                this.header = obj.header;

                switch (obj.header.bit)
                {
                case 16:
                    arrShort = new ushort[obj.soundData.Count];
                    for (int i = 0; i < obj.soundData.Count; i++)
                    {
                        arrShort[i] = (ushort)obj.soundData[i];
                    }
                    break;

                case 32:
                    arrInt = new uint[obj.soundData.Count];
                    for (int i = 0; i < obj.soundData.Count; i++)
                    {
                        arrInt[i] = (uint)obj.soundData[i];
                    }
                    break;

                case 64:
                    arrDouble = new double[obj.soundData.Count];
                    for (int i = 0; i < obj.soundData.Count; i++)
                    {
                        arrDouble[i] = (double)obj.soundData[i];
                    }
                    break;

                default:
                    break;
                }
            }
コード例 #2
0
        public WaveFileObject[] ToWaveChunksWithOverlaps(int ms, WaveFileObject obj)
        {
            double secs         = ms / 1000d;
            long   chunkSamples = (long)(obj.header.sampleRate * secs);
            long   chunkCount   = (((obj.header.dataSize / obj.header.blockSize) / chunkSamples) * 2) - 2;

            WaveFileObject[] waves = new WaveFileObject[chunkCount];
            int osIndex            = 0;

            for (int i = 0; i < chunkCount; i++)
            {
                waves[i] = new WaveFileObject();
                WavHeader tempHeader = header;

                if (header.bit == 16)
                {
                    tempHeader.dataSize  = (uint)chunkSamples * 2;
                    tempHeader.blockSize = 2;
                    waves[i].soundData   = new List <ushort>();
                }
                if (header.bit == 32)
                {
                    tempHeader.dataSize  = (uint)chunkSamples * 4;
                    tempHeader.blockSize = 4;
                    waves[i].soundData   = new List <uint>();
                }
                if (header.bit == 64)
                {
                    tempHeader.dataSize  = (uint)chunkSamples * 8;
                    tempHeader.blockSize = 8;
                    waves[i].soundData   = new List <double>();
                }

                waves[i].header = tempHeader;
            }

            for (int i = 0; i < chunkCount; i++)
            {
                for (int j = 0; j < chunkSamples; j++)
                {
                    if (!IsOdd(i))
                    {
                        waves[i].soundData.Add(obj.soundData[osIndex]);
                        osIndex++;
                    }
                    else
                    {
                        waves[i].soundData.Add(obj.soundData[osIndex + j - (int)(chunkSamples * .5)]);
                    }
                }
            }
            return(waves);
        }
コード例 #3
0
        public static void WriteWaveFile(WaveFileObject obj, string path)
        {
            using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
                using (BinaryWriter bw = new BinaryWriter(fs))
                {
                    try
                    {
                        bw.Write(obj.header.riff);
                        bw.Write(obj.header.size);
                        bw.Write(obj.header.wavID);
                        bw.Write(obj.header.fmtID);
                        bw.Write(obj.header.fmtSize);
                        bw.Write(obj.header.format);
                        bw.Write(obj.header.channels);
                        bw.Write(obj.header.sampleRate);
                        bw.Write(obj.header.bytePerSec);
                        bw.Write(obj.header.blockSize);
                        bw.Write(obj.header.bit);
                        bw.Write(obj.header.dataID);
                        bw.Write(obj.header.dataSize);

                        if (obj.header.bit == 16)
                        {
                            for (int i = 0; i < obj.header.dataSize / obj.header.blockSize; i++)
                            {
                                if (i < obj.soundData.Count)
                                {
                                    bw.Write((ushort)obj.soundData[i]);
                                }
                                else
                                {
                                    bw.Write(0);
                                }
                            }
                        }

                        if (obj.header.bit == 32)
                        {
                            for (int i = 0; i < obj.header.dataSize / obj.header.blockSize; i++)
                            {
                                if (i < obj.soundData.Count)
                                {
                                    bw.Write((uint)obj.soundData[i]);
                                }
                                else
                                {
                                    bw.Write(0);
                                }
                            }
                        }

                        if (obj.header.bit == 64)
                        {
                            for (int i = 0; i < obj.header.dataSize / obj.header.blockSize; i++)
                            {
                                if (i < obj.soundData.Count)
                                {
                                    bw.Write((double)obj.soundData[i]);
                                }
                                else
                                {
                                    bw.Write(0);
                                }
                            }
                        }
                    }
                    finally
                    {
                        if (bw != null)
                        {
                            bw.Close();
                        }
                        if (fs != null)
                        {
                            fs.Close();
                        }
                    }
                }
            return;
        }
コード例 #4
0
        public static WaveFileObject ReadWaveFile(string path)
        {
            WaveFileObject tempObj = new WaveFileObject();

            using (FileStream fs = new FileStream(path, FileMode.Open))
                using (BinaryReader br = new BinaryReader(fs))
                {
                    try
                    {
                        tempObj.header.riff  = br.ReadBytes(4);
                        tempObj.header.size  = br.ReadUInt32();
                        tempObj.header.wavID = br.ReadBytes(4);
                        byte[] temp  = br.ReadBytes(4);
                        string chunk = System.Text.Encoding.UTF8.GetString(temp);
                        if (chunk != "fmt ")
                        {
                            byte[] junk = br.ReadBytes(36);
                        }
                        else
                        {
                            tempObj.header.fmtID = temp;
                        }
                        tempObj.header.fmtSize    = br.ReadUInt32();
                        tempObj.header.format     = br.ReadUInt16();
                        tempObj.header.channels   = br.ReadUInt16();
                        tempObj.header.sampleRate = br.ReadUInt32();
                        tempObj.header.bytePerSec = br.ReadUInt32();
                        tempObj.header.blockSize  = br.ReadUInt16();
                        tempObj.header.bit        = br.ReadUInt16();
                        tempObj.header.dataID     = br.ReadBytes(4);
                        tempObj.header.dataSize   = br.ReadUInt32();

                        if (tempObj.header.bit == 16)
                        {
                            tempObj.soundData = new List <ushort>();

                            for (int i = 0; i < tempObj.header.dataSize / tempObj.header.blockSize; i++)
                            {
                                tempObj.soundData.Add(br.ReadUInt16());
                            }
                        }

                        if (tempObj.header.bit == 32)
                        {
                            tempObj.soundData = new List <uint>();

                            for (int i = 0; i < tempObj.header.dataSize / tempObj.header.blockSize; i++)
                            {
                                tempObj.soundData.Add(br.ReadUInt32());
                            }
                        }

                        if (tempObj.header.bit == 64)
                        {
                            tempObj.soundData = new List <double>();

                            for (int i = 0; i < tempObj.header.dataSize / tempObj.header.blockSize; i++)
                            {
                                tempObj.soundData.Add(br.ReadDouble());
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        throw;
                    }
                    finally
                    {
                        if (br != null)
                        {
                            br.Close();
                        }
                        if (fs != null)
                        {
                            fs.Close();
                        }
                    }
                }
            return(tempObj);
        }