Beispiel #1
0
            public SafeStream GetSubStream()
            {
                FixedLengthSubStream subStream  = new FixedLengthSubStream(m_Stream, Length);
                SafeStream           safeStream = new SafeStream(subStream);

                return(safeStream);
            }
Beispiel #2
0
 public FourByteId(SafeStream stream)
 {
     stream.Read(m_Data, 0, 4);
 }
Beispiel #3
0
 public ChunkHeader(SafeStream stream)
 {
     m_Stream = stream;
     m_TypeId = new FourByteId(stream);
     m_Length = ReadI4(m_Stream);
 }
Beispiel #4
0
 protected Sample ReadSI2(SafeStream stream)
 {
     stream.Read(m_ByteBuffer, 0, 4);
     return(new Sample((short)(m_ByteBuffer[0] | (m_ByteBuffer[1] << 8)),
                       (short)(m_ByteBuffer[2] | (m_ByteBuffer[3] << 8))));
 }
Beispiel #5
0
        public override Wavefile Load(Stream fileStream)
        {
            SafeStream safeStream = new SafeStream(fileStream);

            try
            {
                ChunkHeader riffHeader = new ChunkHeader(safeStream);
                if (!riffHeader.IsType(RiffId))
                {
                    throw new InvalidFileFormatException("WAV-file does not begin with `RIFF'.");
                }
                SafeStream stream = riffHeader.GetSubStream();
                try
                {
                    FourByteId waveHeader = new FourByteId(stream);
                    if (!waveHeader.Equals(WaveId))
                    {
                        throw new InvalidFileFormatException("Riff file is not a waveform.");
                    }

                    while (true)                     //!m_Done)
                    {
                        ChunkHeader chunk = new ChunkHeader(stream);

                        if (chunk.IsType(FormatId))
                        {
                            using (SafeStream formatStream = chunk.GetSubStream())
                            {
                                int formatTag = ReadI2(formatStream);
                                if (formatTag != 1)
                                {
                                    throw new InvalidFileFormatException("Wavefile is not PCM.");
                                }

                                fmt_Channels = ReadI2(formatStream);
                                //if (fmt_channels != 1) throw "Wavefile must be mono." ;
                                //^^^ Fixed 19.Mar.2001

                                fmt_SampleRate = ReadI4(formatStream);

                                ReadI4(formatStream);                                  // <-- average bytes per second (rubbish)
                                ReadI2(formatStream);                                  // <-- block align (who knows)

                                fmt_BitsPerSample = ReadI2(formatStream);

                                m_DoneFormat = true;
                            }
                        }
                        else if (chunk.IsType(DataId))
                        {
                            if (!m_DoneFormat)
                            {
                                throw new InvalidFileFormatException("No format block before data!");
                            }

                            Wavefile wave = new Wavefile();
                            wave.m_SampleRate = (float)fmt_SampleRate;

                            switch (fmt_BitsPerSample)
                            {
                            case 8:
                            {
                                if (fmt_Channels == 2)
                                {
                                    //read_pcm_8bit_st (f, chunk.len/2, wave) ;
                                    //read_pcm = read_pcm_8bit_st ;
                                    m_Length = chunk.Length / 2;
                                    //start() ;
                                }
                                else
                                {
                                    //read_pcm_8bit (f, chunk.len, wave) ;
                                    //read_pcm = read_pcm_8bit ;
                                    m_Length = chunk.Length;
                                    //start() ;
                                }
                            } break;

                            case 16:
                            {
                                if (fmt_Channels == 2)
                                {
                                    //read_pcm_16bit_i_st (f, chunk.len/2/2, wave) ;
                                    //read_pcm = read_pcm_16bit_i_st ;
                                    m_Length = chunk.Length / 2 / 2;
                                    //start() ;
                                }
                                else
                                {
                                    //read_pcm_16bit_i (f, chunk.len/2, wave) ;
                                    //read_pcm = read_pcm_16bit_i ;
                                    m_Length = chunk.Length / 2;
                                    //start() ;
                                }
                            }
                            break;

                            default: throw new InvalidFileFormatException("Must be 8- or 16-bit.");
                            }

                            ThreadPool.QueueUserWorkItem(delegate
                            {
                                using (SafeStream dataStream = chunk.GetSubStream())
                                {
                                    ReadPcm(wave, dataStream, m_Length, fmt_BitsPerSample, fmt_Channels, true);
                                }

                                //m_Done = true;
                                wave.FinishedLoading = true;
                            });
                            safeStream = null;
                            stream     = null;
                            return(wave);
                        }
                        else
                        {
                            chunk.GetSubStream().Dispose();

                            Trace.WriteLine(String.Format("Warning: Unknown chunk `{0}'", chunk.TypeId));;
                        }
                    }
                }
                finally
                {
                    if (stream != null)
                    {
                        stream.Dispose();
                    }
                }
            }
            finally
            {
                if (safeStream != null)
                {
                    safeStream.Dispose();
                }
            }
        }
Beispiel #6
0
 protected short ReadI2(SafeStream stream)
 {
     //return (short)(stream.ReadByte() | (stream.ReadByte() << 8));
     stream.Read(m_ByteBuffer, 0, 2);
     return((short)(m_ByteBuffer[0] | (m_ByteBuffer[1] << 8)));
 }
Beispiel #7
0
 static protected int ReadI4(SafeStream stream)
 {
     return(stream.ReadByte() | (stream.ReadByte() << 8) | (stream.ReadByte() << 16) | (stream.ReadByte() << 24));
 }
Beispiel #8
0
        protected void ReadPcm(Wavefile wave, SafeStream stream, int len,
                               int bits, int channels, bool intel)
        {
            //if (!wave->dontdelete_) sample_free(wave->idata_) ;
            //wave.m_Samples = new List<Sample>(len);
            wave.m_Samples = new BlockArray <Sample>(12);

            //int (*readfun)(Wavefile::Sample *, Stream *, int) ;

            ReadSample readFunction;

            if (bits == 16)
            {
                if (intel)                 // Intel byte order
                {
                    if (channels == 1)
                    {
                        //readfun = f_read_pcm_16bit_i ;
                        readFunction = new ReadSample(ReadPcmI16Mono);
                    }
                    else if (channels == 2)
                    {
                        //readfun = f_read_pcm_16bit_i_st ;
                        readFunction = new ReadSample(ReadPcmI16Stereo);
                    }
                    else
                    {
                        throw new InvalidOperationException();
                    }
                }
                else                 // Motorola byte order
                {
                    if (channels == 1)
                    {
                        //readfun = f_read_pcm_16bit_m ;
                        throw new NotImplementedException();
                    }
                    else if (channels == 2)
                    {
                        //readfun = f_read_pcm_16bit_m_st ;
                        throw new NotImplementedException();
                    }
                    else
                    {
                        throw new InvalidOperationException();
                    }
                }
            }
            else if (bits == 8)
            {
                if (channels == 1)
                {
                    //readfun = f_read_pcm_8bit ;
                    throw new NotImplementedException();
                }
                else if (channels == 2)
                {
                    //readfun = f_read_pcm_8bit_st ;
                    throw new NotImplementedException();
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }
            else
            {
                throw new InvalidOperationException();
            }

            //int a = 0 ;
            //while (a<len) {
            //    //int numread = readfun(wave->idata_+a, file, (len-a) <? BufSize) ;
            //    //if (numread==0) break ;
            //    a += numread ;
            //}

            Sample[] sample = new Sample[1];
            for (int i = 0; i < len; i++)
            {
                sample[0] = readFunction(stream);
                wave.m_Samples.Write(sample);
            }
        }
Beispiel #9
0
        Sample ReadPcmI16Mono(SafeStream stream)
        {
            short value = ReadI2(stream);

            return(new Sample(value, value));
        }
Beispiel #10
0
 Sample ReadPcmI16Stereo(SafeStream stream)
 {
     return(new Sample(ReadI2(stream), ReadI2(stream)));
 }