Exemple #1
0
 public void append(double[] L, double[] R, int length)
 {
     try {
         if (m_bit_per_sample == 8)
         {
             if (m_channel == 1)
             {
                 for (int i = 0; i < length; i++)
                 {
                     m_stream.WriteByte((byte)((L[i] + R[i] + 2.0) * 63.75));
                 }
             }
             else
             {
                 for (int i = 0; i < length; i++)
                 {
                     m_stream.WriteByte((byte)((L[i] + 1.0) * 127.5));
                     m_stream.WriteByte((byte)((R[i] + 1.0) * 127.5));
                 }
             }
         }
         else
         {
             byte[] buf;
             if (m_channel == 1)
             {
                 for (int i = 0; i < length; i++)
                 {
                     buf = PortUtil.getbytes_int16_le((short)((L[i] + R[i]) * 16384.0));
                     writeByteArray(m_stream, buf, 2);
                 }
             }
             else
             {
                 for (int i = 0; i < length; i++)
                 {
                     buf = PortUtil.getbytes_int16_le((short)(L[i] * 32768.0));
                     writeByteArray(m_stream, buf, 2);
                     buf = PortUtil.getbytes_int16_le((short)(R[i] * 32768.0));
                     writeByteArray(m_stream, buf, 2);
                 }
             }
         }
         m_total_samples += (int)length;
     } catch (Exception ex) {
         serr.println("WaveWriter#append(double[],double[],int); ex=" + ex);
     }
 }
Exemple #2
0
        public void append(short[] L, short[] R)
        {
            int total = Math.Min(L.Length, R.Length);

            if (m_bit_per_sample == 8)
            {
                if (m_channel == 1)
                {
                    for (int i = 0; i < total; i++)
                    {
                        m_stream.WriteByte((byte)(((L[i] + R[i]) / 2f + 32768f) / 255f));
                    }
                }
                else
                {
                    for (int i = 0; i < total; i++)
                    {
                        m_stream.WriteByte((byte)((L[i] + 32768f) / 255f));
                        m_stream.WriteByte((byte)((R[i] + 32768f) / 255f));
                    }
                }
            }
            else
            {
                byte[] buf;
                if (m_channel == 1)
                {
                    for (int i = 0; i < total; i++)
                    {
                        buf = PortUtil.getbytes_int16_le((short)((L[i] + R[i]) / 2));
                        writeByteArray(m_stream, buf, 2);
                    }
                }
                else
                {
                    for (int i = 0; i < total; i++)
                    {
                        buf = PortUtil.getbytes_int16_le(L[i]);
                        writeByteArray(m_stream, buf, 2);
                        buf = PortUtil.getbytes_int16_le(R[i]);
                        writeByteArray(m_stream, buf, 2);
                    }
                }
            }
            m_total_samples += (int)total;
        }
Exemple #3
0
        public void append(float[] L, float[] R)
        {
            int total = Math.Min(L.Length, R.Length);

            if (m_bit_per_sample == 8)
            {
                if (m_channel == 1)
                {
                    for (int i = 0; i < total; i++)
                    {
                        m_stream.WriteByte((byte)((L[i] + R[i] + 2.0f) * 63.75f));
                    }
                }
                else
                {
                    for (int i = 0; i < total; i++)
                    {
                        m_stream.WriteByte((byte)((L[i] + 1.0f) * 127.5f));
                        m_stream.WriteByte((byte)((R[i] + 1.0f) * 127.5f));
                    }
                }
            }
            else
            {
                byte[] buf;
                if (m_channel == 1)
                {
                    for (int i = 0; i < total; i++)
                    {
                        buf = PortUtil.getbytes_int16_le((short)((L[i] + R[i]) * 16384f));
                        writeByteArray(m_stream, buf, 2);
                    }
                }
                else
                {
                    for (int i = 0; i < total; i++)
                    {
                        buf = PortUtil.getbytes_int16_le((short)(L[i] * 32768f));
                        writeByteArray(m_stream, buf, 2);
                        buf = PortUtil.getbytes_int16_le((short)(R[i] * 32768f));
                        writeByteArray(m_stream, buf, 2);
                    }
                }
            }
            m_total_samples += (int)total;
        }
Exemple #4
0
        public void append(double[] L)
        {
            int total = L.Length;

            if (m_bit_per_sample == 8)
            {
                if (m_channel == 1)
                {
                    for (int i = 0; i < total; i++)
                    {
                        m_stream.WriteByte((byte)((L[i] + 1.0) * 127.5));
                    }
                }
                else
                {
                    for (int i = 0; i < total; i++)
                    {
                        byte b = (byte)((L[i] + 1.0) * 127.5);
                        m_stream.WriteByte(b);
                        m_stream.WriteByte(b);
                    }
                }
            }
            else
            {
                byte[] buf;
                if (m_channel == 1)
                {
                    for (int i = 0; i < total; i++)
                    {
                        buf = PortUtil.getbytes_int16_le((short)(L[i] * 32768.0));
                        writeByteArray(m_stream, buf, 2);
                    }
                }
                else
                {
                    for (int i = 0; i < total; i++)
                    {
                        buf = PortUtil.getbytes_int16_le((short)(L[i] * 32768.0));
                        writeByteArray(m_stream, buf, 2);
                        writeByteArray(m_stream, buf, 2);
                    }
                }
            }
            m_total_samples += (int)total;
        }
Exemple #5
0
        /// <summary>
        /// 第posサンプルからlengthサンプル分、指定した波形データで置き換えます
        /// </summary>
        /// <param name="pos"></param>
        /// <param name="length"></param>
        /// <param name="L"></param>
        /// <param name="R"></param>
        public void replace(long pos, int length, double[] L, double[] R)
        {
            long lastPos   = m_stream.Position;
            long posFile   = pos * m_channel * m_bit_per_sample / 8 + m_pos_data_chunk;
            long streamLen = m_stream.Length;

            if (streamLen < posFile)
            {
                // ファイルの長さが足りていない場合、とりあえず0で埋める。
                m_stream.Seek(streamLen - 1, SeekOrigin.Begin);
                long   remain = posFile - streamLen;
                int    buflen = 1024;
                byte[] data   = new byte[buflen];
                while (remain > 0)
                {
                    int delta = remain > buflen ? buflen : (int)remain;
                    m_stream.Write(data, 0, delta);
                    remain -= delta;
                }
                m_total_samples = pos;
            }
            m_stream.Seek(posFile, SeekOrigin.Begin);

            // 書き込み
            if (m_bit_per_sample == 8)
            {
                if (m_channel == 1)
                {
                    for (int i = 0; i < length; i++)
                    {
                        m_stream.WriteByte((byte)((L[i] + R[i] + 2.0) * 63.75));
                    }
                }
                else
                {
                    for (int i = 0; i < length; i++)
                    {
                        m_stream.WriteByte((byte)((L[i] + 1.0) * 127.5));
                        m_stream.WriteByte((byte)((R[i] + 1.0) * 127.5));
                    }
                }
            }
            else
            {
                byte[] buf;
                if (m_channel == 1)
                {
                    for (int i = 0; i < length; i++)
                    {
                        buf = PortUtil.getbytes_int16_le((short)((L[i] + R[i]) * 16384.0));
                        writeByteArray(m_stream, buf, 2);
                    }
                }
                else
                {
                    for (int i = 0; i < length; i++)
                    {
                        buf = PortUtil.getbytes_int16_le((short)(L[i] * 32768.0));
                        writeByteArray(m_stream, buf, 2);
                        buf = PortUtil.getbytes_int16_le((short)(R[i] * 32768.0));
                        writeByteArray(m_stream, buf, 2);
                    }
                }
            }
            m_total_samples = (m_total_samples < pos + length) ? (pos + length) : (m_total_samples);

            // 最後にファイルポインタを戻す
            m_stream.Seek(lastPos, SeekOrigin.Begin);
        }