File: WaveSenderDriver.cs

package info (click to toggle)
cadencii 3.3.9%2Bsvn20110818.r1732-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 35,764 kB
  • ctags: 26,929
  • sloc: cs: 160,836; java: 42,449; cpp: 7,605; ansic: 1,728; perl: 1,087; makefile: 236; php: 142; xml: 117; sh: 21
file content (158 lines) | stat: -rw-r--r-- 4,358 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * WaveSenderDriver.cs
 * Copyright © 2010-2011 kbinani
 *
 * This file is part of org.kbinani.cadencii.
 *
 * org.kbinani.cadencii is free software; you can redistribute it and/or
 * modify it under the terms of the GPLv3 License.
 *
 * org.kbinani.cadencii is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */
#if JAVA
package org.kbinani.cadencii;

import java.awt.*;
import java.util.*;
import org.kbinani.*;
#else
using System;
using System.Threading;
using org.kbinani.java.awt;
using org.kbinani.java.util;

namespace org.kbinani.cadencii
{
    using boolean = System.Boolean;
#endif

    /// <summary>
    /// WaveSenderをWaveGeneratorとして使うためのドライバー.
    /// WaveSenderは受動的波形生成器なので,自分では波形を作らない.
    /// </summary>
#if JAVA
    public class WaveSenderDriver extends WaveUnit implements WaveGenerator {
#else
    public class WaveSenderDriver : WaveUnit, WaveGenerator
    {
#endif
        private const int BUFLEN = 1024;

        private WaveSender mWaveSender = null;
        private double[] mBufferL = new double[BUFLEN];
        private double[] mBufferR = new double[BUFLEN];
        private long mTotalAppend = 0;
        private long mTotalSamples = 1L;
        private WaveReceiver mReceiver = null;
        private int mVersion = 0;
        //private boolean mAbortRequired = false;
        private boolean mRunning = false;
        private int mSampleRate;

        public int getSampleRate()
        {
            return mSampleRate;
        }

        public boolean isRunning()
        {
            return mRunning;
        }

        public long getTotalSamples()
        {
            return mTotalSamples;
        }

        public double getProgress()
        {
            if ( mTotalSamples <= 0 ) {
                return 0.0;
            } else {
                return mTotalAppend / (double)mTotalSamples;
            }
        }

        /*public void stop()
        {
            if ( mRunning ) {
                mAbortRequired = true;
                while ( mRunning ) {
#if JAVA
                    try{
                        Thread.sleep( 100 );
                    }catch( Exception ex ){
                    }
#else
                    Thread.Sleep( 100 );
#endif
                }
            }
        }*/

        public override int getVersion()
        {
            return mVersion;
        }

        public override void setConfig( String parameters )
        {
            // do nothing
        }

        /// <summary>
        /// 初期化メソッド
        /// </summary>
        /// <param name="vsq"></param>
        /// <param name="track"></param>
        /// <param name="start_clock"></param>
        /// <param name="end_clock"></param>
        public void init( VsqFileEx vsq, int track, int start_clock, int end_clock, int sample_rate )
        {
            mSampleRate = sample_rate;
#if DEBUG
            sout.println( "WaveSenderDriver#init; sample_rate=" + sample_rate );
#endif
        }

        public void setSender( WaveSender wave_sender )
        {
            mWaveSender = wave_sender;
        }

        public void setReceiver( WaveReceiver r )
        {
            if ( mReceiver != null ) {
                mReceiver.end();
            }
            mReceiver = r;
        }

        public long getPosition()
        {
            return mTotalAppend;
        }

        public void begin( long length, WorkerState state )
        {
            mRunning = true;
            mTotalSamples = length;
            long remain = length;
            while ( remain > 0 && !state.isCancelRequested() ) {
                int amount = (remain > BUFLEN) ? BUFLEN : (int)remain;
                mWaveSender.pull( mBufferL, mBufferR, amount );
                mReceiver.push( mBufferL, mBufferR, amount );
                remain -= amount;
                mTotalAppend += amount;
            }
            mWaveSender.end();
            mReceiver.end();
            mRunning = false;
        }
    }

#if !JAVA
}
#endif