예제 #1
0
        public static RiffStream OpenWrite(Stream underlyingStream, WaveFormat2 fmt)
        {
            RiffStream stm = new RiffStream();

            stm._accessMode = FileAccess.Write;
            stm._baseStream = underlyingStream;
            stm.InitializeForWriting(underlyingStream, fmt, fmt, false);
            stm.WriteHeader();
            return(stm);
        }
예제 #2
0
        public static RiffStream Append(Stream underlyingStream)
        {
            RiffStream stm = new RiffStream();

            stm._accessMode          = FileAccess.Write;
            stm._baseStream          = underlyingStream;
            stm._baseStream.Position = 0;
            WaveFormat2 fmtOut = WaveFormat2.FromStream(underlyingStream);

            stm._baseStream.Position = stm._baseStream.Length;
            stm.InitializeForWriting(underlyingStream, fmtOut, fmtOut, true);
            stm.WriteHeader();
            return(stm);
        }
예제 #3
0
        public static RiffStream OpenRead(Stream underlyingStream)
        {
            RiffStream stm = new RiffStream();

            stm._accessMode = FileAccess.Read;
            stm._baseStream = underlyingStream;
            stm.LoadHeader();
            if (stm.Format.FormatTag != FormatTag.PCM) // need conversion
            {
                stm.Detach();
                underlyingStream.Seek(0, SeekOrigin.Begin);
                stm = ACMStream.OpenRead(underlyingStream);
            }
            return(stm);
        }
예제 #4
0
        /// <summary>
        /// Record sound data for specified number of seconds using given wave format
        /// The stream will be a properly formatted RIFF file
        /// </summary>
        /// <param name="st">Stream into which recorded samples are written</param>
        /// <param name="Seconds">Seconds of data to record</param>
        /// <param name="format">Sound format to record in.</param>
        public RiffStream RecordFor(Stream st, short Seconds, WaveFormat2 format)
        {
            // only allow 1 recording session at a time
            if (recording)
            {
                throw new InvalidOperationException("Already recording");
            }

            m_hWaveIn = IntPtr.Zero;

            // set our global flag
            recording = true;

            if (m_qBuffers == null)
            {
                m_qBuffers = new Queue <WaveHeader>(MaxBuffers);
            }
            if (m_HandleMap == null)
            {
                m_HandleMap = new System.Collections.Hashtable(MaxBuffers);
            }

            m_recformat = new WaveFormat2();
#if !NDOC
            // create the callback message window
            m_recmw = new SoundMessageWindow();
            m_recmw.WaveDoneMessage  += new WaveDoneHandler(m_recmw_WaveDoneMessage);
            m_recmw.WaveCloseMessage += new WaveCloseHandler(mw_WaveCloseMessage);
            m_recmw.WaveOpenMessage  += new WaveOpenHandler(mw_WaveOpenMessage);
#endif
            bool append = st.Length >= 42;
            if (format.FormatTag == FormatTag.PCM)
            {
                m_recformat = format;
                if (append)
                {
                    m_streamRecord = RiffStream.Append(st);
                }
                else
                {
                    m_streamRecord = RiffStream.OpenWrite(st, m_recformat);
                }
            }
            else
            {
                m_recformat = WaveFormat2.GetPCMWaveFormat(SoundFormats.Mono8bit11kHz);
                if (append)
                {
                    m_streamRecord = ACMStream.Append(st, m_recformat);
                }
                else
                {
                    m_streamRecord = ACMStream.OpenWrite(st, m_recformat, format);
                }
            }
#if !NDOC
            // check for support of selected format
            CheckWaveError(NativeMethods.waveInOpen(out m_hWaveIn, WAVE_MAPPER, m_recformat.GetBytes(), IntPtr.Zero, 0, WAVE_FORMAT_QUERY));

            // open wave device
            CheckWaveError(NativeMethods.waveInOpen(out m_hWaveIn, (uint)m_deviceID, m_recformat.GetBytes(), m_recmw.Hwnd, 0, CALLBACK_WINDOW));

            m_recBufferSize = (int)(Math.Min((int)Seconds, BufferLen) * m_recformat.AvgBytesPerSec);

            for (int i = 0; i < 2; i++)
            {
                WaveHeader hdr = GetNewRecordBuffer(m_recBufferSize);

                // send the buffer to the device
                CheckWaveError(NativeMethods.waveInAddBuffer(m_hWaveIn, hdr.Pointer, hdr.HeaderLength));
            }

            // begin recording
            CheckWaveError(NativeMethods.waveInStart(m_hWaveIn));
            recordingFinished = false;
            m_recTimer        = new Timer(new TimerCallback(RecTimerCallback), this, Seconds * 1000, Timeout.Infinite);
#endif
            return(m_streamRecord);
        }