1 module stk.instrument;
2 
3 import stk.stk;
4 
5 /**
6 
7 STK instrument abstract base class.
8 
9 This class provides a common interface for
10 all STK instruments.
11 
12 Porting portions of STK library to D.
13 https://github.com/thestk/stk by Perry R. Cook and Gary P. Scavone, 1995--2016.
14 
15 */
16 
17 abstract class Instrmnt : Stk
18 {
19 public:
20     //! Class constructor.
21     this() {
22         lastFrame_.resize( 1, 1, 0.0 );
23     };
24 
25     //! Reset and clear all internal state (for subclasses).
26     /*!
27     Not all subclasses implement a clear() function.
28     */
29     void clear() {};
30 
31     //! Start a note with the given frequency and amplitude.
32     void noteOn( StkFloat frequency, StkFloat amplitude ) = 0;
33 
34     //! Stop a note with the given amplitude (speed of decay).
35     void noteOff( StkFloat amplitude ) = 0;
36 
37     //! Set instrument parameters for a particular frequency.
38     void setFrequency( StkFloat frequency );
39 
40     //! Perform the control change specified by \e number and \e value (0.0 - 128.0).
41     void controlChange(int number, StkFloat value);
42 
43     //! Return the number of output channels for the class.
44     uint channelsOut() const { return lastFrame_.channels(); };
45 
46     //! Return an StkFrames reference to the last output sample frame.
47     ref StkFrames lastFrame() { return lastFrame_; };
48 
49     //! Return the specified channel value of the last computed frame.
50     /*!
51     The \c channel argument must be less than the number of output
52     channels, which can be determined with the channelsOut() function
53     (the first channel is specified by 0).  However, range checking is
54     only performed if _STK_DEBUG_ is defined during compilation, in
55     which case an out-of-range value will trigger an StkError
56     exception. \sa lastFrame()
57     */
58     StkFloat lastOut( uint channel = 0 );
59 
60     //! Compute one sample frame and return the specified \c channel value.
61     /*!
62     For monophonic instruments, the \c channel argument is ignored.
63     */
64     StkFloat tick( uint channel = 0 ) = 0;
65 
66     //! Fill the StkFrames object with computed sample frames, starting at the specified channel.
67     /*!
68     The \c channel argument plus the number of output channels must
69     be less than the number of channels in the StkFrames argument (the
70     first channel is specified by 0).  However, range checking is only
71     performed if _STK_DEBUG_ is defined during compilation, in which
72     case an out-of-range value will trigger an StkError exception.
73     */
74     StkFrames tick( StkFrames frames, uint channel = 0) = 0;
75 
76 protected:
77 
78     StkFrames lastFrame_;
79 
80 }