Пример #1
0
 public void pull(double[] l, double[] r, int length)
 {
     lock (mSyncRoot) {
         if (mConverter == null)
         {
             int rate = mRoot.getSampleRate();
             mConverter = new WaveRateConverter(mReader, rate);
         }
         try {
             mConverter.read(mPosition, length, l, r);
             mPosition += length;
         } catch (Exception ex) {
             serr.println("FileWaveSender#pull; ex=" + ex);
             Logger.write(typeof(FileWaveSender) + ".pull; ex=" + ex + "\n");
         }
     }
 }
Пример #2
0
        /// <summary>
        /// 指定したファイルの指定した区間を追加で読み込みます
        /// </summary>
        /// <param name="file"></param>
        /// <param name="sec_from"></param>
        /// <param name="sec_to"></param>
        public void reloadPartial(string file, double sec_from, double sec_to)
        {
            if (!System.IO.File.Exists(file))
            {
                return;
            }

            WaveRateConverter wr = null;

            try {
                wr = new WaveRateConverter(new WaveReader(file), mSampleRate);
                int saFrom = (int)(sec_from * mSampleRate);
                int saTo   = (int)(sec_to * mSampleRate);

                // バッファを確保
                int      buflen = 1024;
                double[] left   = new double[buflen];
                double[] right  = new double[buflen];

                // まず、読み込んだ区間の最大振幅を調べる
                int    remain = saTo - saFrom;
                int    pos    = saFrom;
                double max    = 0.0;
                while (remain > 0)
                {
                    int delta = remain > buflen ? buflen : remain;
                    wr.read(pos, delta, left, right);
                    for (int i = 0; i < delta; i++)
                    {
                        double d = Math.Abs((left[i] + right[i]) * 0.5);
                        max = d > max ? d : max;
                    }
                    remain -= delta;
                    pos    += delta;
                }

                // バッファが足りなければ確保
                int oldLength = mWave.Length;
                if (oldLength < saTo)
                {
                    Array.Resize(ref mWave, saTo);
                    saFrom = oldLength;
                }

                if (mMaxAmplitude < max)
                {
                    // 既存の波形の最大振幅より、読み込み部分の最大波形が大きいようなら、
                    // 既存波形の縮小を行う
                    double ampall = 1.0 / max;
                    for (int i = 0; i < mWave.Length; i++)
                    {
                        double vold = mWave[i] / 127.0 * mMaxAmplitude;
                        double vnew = vold * ampall;
                        mWave[i] = (sbyte)(vnew * 127);
                    }
                }

                // 最大振幅の値を更新
                mMaxAmplitude = (max > mMaxAmplitude) ? (float)max : mMaxAmplitude;

                // 今度は波形を取得するために読み込む
                double amp = (mMaxAmplitude > 0.0f) ? (1.0 / mMaxAmplitude) : 0.0;
                remain = saTo - saFrom;
                pos    = saFrom;
                while (remain > 0)
                {
                    int delta = remain > buflen ? buflen : remain;
                    wr.read(pos, delta, left, right);

                    for (int i = 0; i < delta; i++)
                    {
                        double d = (left[i] + right[i]) * 0.5 * amp;
                        sbyte  b = (sbyte)(d * 127);
                        mWave[pos + i] = b;
                    }

                    pos    += delta;
                    remain -= delta;
                }
                left  = null;
                right = null;

                // mActualMaxAmplitudeの値を更新
                mActualMaxAmplitude = 0.0f;
                for (int i = 0; i < mWave.Length; i++)
                {
                    double d = Math.Abs(mWave[i] / 127.0 * mMaxAmplitude);
                    mActualMaxAmplitude = (d > mActualMaxAmplitude) ? (float)d : mActualMaxAmplitude;
                }
            } catch (Exception ex) {
                serr.println("WaveDrawContext#reloadPartial; ex=" + ex);
            } finally {
                if (wr != null)
                {
                    try {
                        wr.close();
                    } catch (Exception ex2) {
                        serr.println("WaveDrawContext#reloadPartial; ex2=" + ex2);
                    }
                }
            }
        }