Beispiel #1
0
            public static Chunk ReadChunk(ref BinaryReader reader)
            {
                int data = reader.ReadInt32();

                if (data == 0x46464952) // RIFF chunk
                {
                    RIFFChunk riff = new RIFFChunk();
                    riff.chunk_id  = data;
                    riff.file_size = reader.ReadInt32();
                    riff.riff_type = reader.ReadInt32();
                    return(riff);
                }
                else if (data == 0x20746D66)    // fmt chunk
                {
                    fmtChunk fmt = new fmtChunk();
                    fmt.chunk_id        = data;
                    fmt.chunk_size      = reader.ReadInt32();
                    fmt.format          = reader.ReadInt16();
                    fmt.channels        = reader.ReadInt16();
                    fmt.sample_rate     = reader.ReadInt32();
                    fmt.bytes_per_sec   = reader.ReadInt32();
                    fmt.block_align     = reader.ReadInt16();
                    fmt.bits_per_sample = reader.ReadInt16();
                    return(fmt);
                }
                else if (data == 0x61746164) // data chunk
                {
                    dataChunk data_chunk = new dataChunk();
                    data_chunk.chunk_id   = data;
                    data_chunk.chunk_size = reader.ReadInt32();
                    return(data_chunk);
                }
                else
                {
                    Chunk c = new Chunk();
                    c.chunk_id = data;
                    return(c);
                }
            }
Beispiel #2
0
        unsafe private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            try
            {
                RIFFChunk riff       = null;
                fmtChunk  fmt        = null;
                dataChunk data_chunk = null;

                Audio.loopback = false;

                if (Audio.rb_mon_l == null || Audio.rb_mon_r == null)
                {
                    Audio.rb_mon_l = new RingBufferFloat(Math.Max(buffer_size, 1024) * 1024);
                    Audio.rb_mon_r = new RingBufferFloat(Math.Max(buffer_size, 1024) * 1024);
                }
                else
                {
                    Audio.rb_mon_l.Reset();
                    Audio.rb_mon_r.Reset();
                }

                btnPlay.BackColor = Color.WhiteSmoke;
                btnPlay.Text      = "Play";

                float[] buf_left = new float[2048];
                float[] buf_right = new float[2048];
                byte[]  data = new byte[16384];
                byte[]  temp = new byte[4];
                int     count = 0, buflen = 0;
                string  file        = openFileDialog1.FileName;
                Stream  file_stream = File.Open(file, FileMode.Open, FileAccess.Read);
                reader = new BinaryReader(file_stream);
                string[] vals = file.Split('\\');
                this.Text = " Recorder/Player   " + vals[vals.Length - 1];

                try
                {
                    if (reader.PeekChar() != 'R')
                    {
                        reader.Close();
                        MessageBox.Show("File is not in the correct format.",
                                        "Wrong File Format",
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Error);
                        return;
                    }

                    while ((data_chunk == null ||
                            riff == null || fmt == null) &&
                           reader.PeekChar() != -1)
                    {
                        Chunk chunk = Chunk.ReadChunk(ref reader);
                        if (chunk.GetType() == typeof(RIFFChunk))
                        {
                            riff = (RIFFChunk)chunk;
                        }
                        else if (chunk.GetType() == typeof(fmtChunk))
                        {
                            fmt = (fmtChunk)chunk;
                        }
                        else if (chunk.GetType() == typeof(dataChunk))
                        {
                            data_chunk = (dataChunk)chunk;
                        }
                    }

                    if (reader.PeekChar() == -1)
                    {
                        reader.Close();
                        MessageBox.Show("File is not in the correct format.",
                                        "Wrong File Format",
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Error);
                        return;
                    }

                    if (riff.riff_type != 0x45564157)
                    {
                        reader.Close();
                        MessageBox.Show("File is not an RIFF Wave file.",
                                        "Wrong file format",
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Error);
                        return;
                    }

                    if ((fmt.sample_rate != Audio.SampleRate) || fmt.format == 1)
                    {
                        reader.Close();
                        MessageBox.Show("File has the wrong sample rate.",
                                        "Wrong Sample Rate",
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Error);
                        return;
                    }

                    if (fmt.channels != 2)
                    {
                        reader.Close();
                        MessageBox.Show("Wave File is not stereo.",
                                        "Wrong Number of Channels",
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Error);
                        return;
                    }
                }
                catch (Exception ex)
                {
                    if (reader != null)
                    {
                        reader.Close();
                    }

                    MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace.ToString(), "Fatal Error",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                fixed(float *input_l = &buf_left[0])
                fixed(float *input_r = &buf_right[0])
                {
                    while (Audio.rb_mon_l.WriteSpace() >= 2048)
                    {
                        count  = reader.Read(data, 0, 16384);
                        buflen = Math.Min(count, 16384);

                        for (int i = 0; i < (buflen / 8); i++)
                        {
                            buf_left[i]  = BitConverter.ToSingle(data, (i * 8));
                            buf_right[i] = BitConverter.ToSingle(data, (i * 8 + 4));
                        }

                        Audio.rb_mon_l.WritePtr(input_l, 2048);
                        Audio.rb_mon_r.WritePtr(input_r, 2048);

                        if (buflen < 16384)
                        {
                            reader.Close();
                            return;
                        }
                    }

                    reader.Close();
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.ToString());

                if (reader != null)
                {
                    reader.Close();
                }
            }
        }
Beispiel #3
0
 public static Chunk ReadChunk(ref BinaryReader reader)
 {
     int data = reader.ReadInt32();
     if (data == 0x46464952)	// RIFF chunk
     {
         RIFFChunk riff = new RIFFChunk();
         riff.chunk_id = data;
         riff.file_size = reader.ReadInt32();
         riff.riff_type = reader.ReadInt32();
         return riff;
     }
     else if (data == 0x20746D66)	// fmt chunk
     {
         fmtChunk fmt = new fmtChunk();
         fmt.chunk_id = data;
         fmt.chunk_size = reader.ReadInt32();
         fmt.format = reader.ReadInt16();
         fmt.channels = reader.ReadInt16();
         fmt.sample_rate = reader.ReadInt32();
         fmt.bytes_per_sec = reader.ReadInt32();
         fmt.block_align = reader.ReadInt16();
         fmt.bits_per_sample = reader.ReadInt16();
         return fmt;
     }
     else if (data == 0x61746164) // data chunk
     {
         dataChunk data_chunk = new dataChunk();
         data_chunk.chunk_id = data;
         data_chunk.chunk_size = reader.ReadInt32();
         return data_chunk;
     }
     else
     {
         Chunk c = new Chunk();
         c.chunk_id = data;
         return c;
     }
 }