Exemple #1
0
        public void close()
        {
#if DEBUG
            sout.println("WaveWriter#close; m_path=" + m_path);
#endif
            if (m_stream == null)
            {
                return;
            }
            try {
                // 最後にWAVEチャンクのサイズ
                int position = (int)m_stream.Position;
                m_stream.Seek(4, SeekOrigin.Begin);
                byte[] buf = PortUtil.getbytes_uint32_le(position - 8);
                writeByteArray(m_stream, buf, 4);

                // size of data chunk
                int  block_size = (int)(m_bit_per_sample / 8 * (int)m_channel);
                long size       = block_size * m_total_samples;
                m_stream.Seek(42, SeekOrigin.Begin);
                buf = PortUtil.getbytes_uint32_le(size);
                writeByteArray(m_stream, buf, 4);

                m_stream.Close();
            } catch (Exception ex) {
                serr.println("WaveWriter#close; ex=" + ex);
            }
        }
Exemple #2
0
        /// <summary>
        /// Writes header of WAVE file
        /// </summary>
        private void writeHeader()
        {
            // RIFF
            m_stream.WriteByte(0x52); // loc=0x00
            m_stream.WriteByte(0x49);
            m_stream.WriteByte(0x46);
            m_stream.WriteByte(0x46);

            // ファイルサイズ - 8最後に記入
            m_stream.WriteByte(0x00); // loc=0x04
            m_stream.WriteByte(0x00);
            m_stream.WriteByte(0x00);
            m_stream.WriteByte(0x00);

            // WAVE
            m_stream.WriteByte(0x57); // loc=0x08
            m_stream.WriteByte(0x41);
            m_stream.WriteByte(0x56);
            m_stream.WriteByte(0x45);

            // fmt
            m_stream.WriteByte(0x66); // loc=0x0c
            m_stream.WriteByte(0x6d);
            m_stream.WriteByte(0x74);
            m_stream.WriteByte(0x20);

            // fmt チャンクのサイズ
            m_stream.WriteByte(0x12); // loc=0x10
            m_stream.WriteByte(0x00);
            m_stream.WriteByte(0x00);
            m_stream.WriteByte(0x00);

            // format ID
            m_stream.WriteByte(0x01); // loc=0x14
            m_stream.WriteByte(0x00);

            // チャンネル数
            if (m_channel == 1)
            {
                m_stream.WriteByte(0x01); // loc=0x16
                m_stream.WriteByte(0x00);
            }
            else
            {
                m_stream.WriteByte(0x02); //loc=0x16
                m_stream.WriteByte(0x00);
            }

            // サンプリングレート
            byte[] buf = PortUtil.getbytes_uint32_le(m_sample_rate);
            writeByteArray(m_stream, buf, 4); // 0x18

            // データ速度
            int block_size = (int)(m_bit_per_sample / 8 * (int)m_channel);
            int data_rate  = m_sample_rate * block_size;

            buf = PortUtil.getbytes_uint32_le(data_rate);
            writeByteArray(m_stream, buf, 4);//loc=0x1c

            // ブロックサイズ
            buf = PortUtil.getbytes_uint16_le(block_size);
            writeByteArray(m_stream, buf, 2); //0x20

            // サンプルあたりのビット数
            buf = PortUtil.getbytes_uint16_le(m_bit_per_sample);
            writeByteArray(m_stream, buf, 2); //loc=0x22

            // 拡張部分
            m_stream.WriteByte(0x00); //loc=0x24
            m_stream.WriteByte(0x00);

            // data
            m_stream.WriteByte(0x64); //loc=0x26
            m_stream.WriteByte(0x61);
            m_stream.WriteByte(0x74);
            m_stream.WriteByte(0x61);

            // size of data chunk
            long size = block_size * m_total_samples;

            buf = PortUtil.getbytes_uint32_le(size);
            writeByteArray(m_stream, buf, 4);
            m_pos_data_chunk = m_stream.Position;
        }