~ubuntu-branches/ubuntu/oneiric/muse/oneiric

« back to all changes in this revision

Viewing changes to synti/stklib/RtWvIn.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Kobras
  • Date: 2002-04-23 17:28:23 UTC
  • Revision ID: james.westby@ubuntu.com-20020423172823-w8yplzr81a759xa3
Tags: upstream-0.5.2
ImportĀ upstreamĀ versionĀ 0.5.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************/
 
2
/*  RtWvIn Input Class,                    */
 
3
/*  by Gary P. Scavone, 1999-2000          */
 
4
/*                                         */
 
5
/*  This object inherits from WvIn and is  */
 
6
/*  used to read in realtime 16-bit data   */
 
7
/*  from a computer's audio port.          */
 
8
/*                                         */
 
9
/*  NOTE: This object is NOT intended for  */
 
10
/*  use in achieving simultaneous realtime */
 
11
/*  audio input/output (together with      */
 
12
/*  RtWvOut). Under certain circumstances  */
 
13
/*  such a scheme is possible, though you  */
 
14
/*  should definitely know what you are    */
 
15
/*  doing before trying.  For safer "full- */
 
16
/*  duplex" operation, use the RtDuplex    */
 
17
/*  class.                                 */
 
18
/*******************************************/
 
19
 
 
20
#include "RtWvIn.h"
 
21
 
 
22
RtWvIn :: RtWvIn(int chans, MY_FLOAT srate, int device)
 
23
{
 
24
  chunking = 1;
 
25
  looping = 0;
 
26
  sound_dev = new RtAudio(chans, srate, "record", device);
 
27
  channels = chans;
 
28
  bufferSize = RT_BUFFER_SIZE;
 
29
  data = 0;
 
30
  rtdata = (INT16 *) new INT16[(bufferSize+1)*channels];
 
31
 
 
32
  lastSamples = (INT16 *) new INT16[channels];
 
33
  for (int i=0;i<channels;i++) {
 
34
    lastSamples[i] = (INT16) 0.0;
 
35
  }
 
36
 
 
37
  this->getData(0);
 
38
 
 
39
  rate = (MY_FLOAT) srate / SRATE;
 
40
  if (fmod(rate, 1.0) > 0.0) interpolate = 1;
 
41
  else interpolate = 0;
 
42
  phaseOffset = (MY_FLOAT) 0.0;
 
43
  lastOutput = (MY_FLOAT *) new MY_FLOAT[channels];
 
44
  this->reset();
 
45
 
 
46
  gain = 0.00003052;
 
47
#if (defined(__STK_REALTIME_) && defined(__OS_IRIX_))
 
48
  // This is necessary under IRIX because it scales the input by 0.5
 
49
  // when using single-channel input.
 
50
  if (channels == 1) gain *= 2;
 
51
#endif
 
52
}
 
53
 
 
54
RtWvIn :: ~RtWvIn()
 
55
{
 
56
  delete sound_dev;
 
57
  if (rtdata) {
 
58
    delete [ ] rtdata;
 
59
    rtdata = 0;
 
60
  }
 
61
  if (lastSamples) {
 
62
    delete [ ] lastSamples;
 
63
    lastSamples = 0;
 
64
  }
 
65
}
 
66
 
 
67
void RtWvIn :: setRate(MY_FLOAT aRate)
 
68
{
 
69
  // Negative rates not allowed for realtime input
 
70
  rate = fabs(aRate);
 
71
 
 
72
  if (fmod(rate, 1.0) != 0.0) interpolate = 1;
 
73
  else interpolate = 0;
 
74
}
 
75
 
 
76
void RtWvIn :: addTime(MY_FLOAT aTime)   
 
77
{
 
78
  // Negative time shift no allowed for realtime input
 
79
  time += fabs(aTime);
 
80
}
 
81
 
 
82
void RtWvIn :: setLooping(int aLoopStatus)
 
83
{
 
84
  // No looping for realtime data.
 
85
  looping = 0;
 
86
}
 
87
 
 
88
long RtWvIn :: getSize()
 
89
{
 
90
  return bufferSize;
 
91
}
 
92
 
 
93
void RtWvIn :: getData(long index)
 
94
{
 
95
  static long temp = RT_BUFFER_SIZE*channels;
 
96
  sound_dev->recordBuffer(&rtdata[channels],temp);
 
97
 
 
98
  /* Fill in the extra sample frame for interpolation.
 
99
   * We do this by pre-pending the last sample frame
 
100
   * from the previous input buffer to the current one.
 
101
   */
 
102
  for (int i=0;i<channels;i++) {
 
103
    rtdata[i] = lastSamples[i];
 
104
    lastSamples[i] = rtdata[temp+i];
 
105
  }
 
106
}
 
107
 
 
108
int RtWvIn :: informTick()
 
109
{
 
110
  static MY_FLOAT alpha;
 
111
  static long index;
 
112
 
 
113
  if (time >= bufferSize) {
 
114
    this->getData(0);
 
115
    while (time >= bufferSize)
 
116
      time -= bufferSize;
 
117
  }
 
118
 
 
119
  // integer part of time address
 
120
  index = (long) time;
 
121
 
 
122
  if (interpolate) {
 
123
    // fractional part of time address
 
124
    alpha = time - (MY_FLOAT) index;
 
125
    index *= channels;
 
126
    for (int i=0;i<channels;i++) {
 
127
      //  Do linear interpolation
 
128
      lastOutput[i] = (MY_FLOAT) rtdata[index];
 
129
      lastOutput[i] += alpha * ((MY_FLOAT) rtdata[index+channels] - lastOutput[i]);
 
130
      lastOutput[i] *= gain;
 
131
      index++;
 
132
    }
 
133
  }
 
134
  else {
 
135
    index *= channels;
 
136
    for (int i=0;i<channels;i++) {
 
137
      lastOutput[i] = rtdata[index++];
 
138
      lastOutput[i] *= gain;
 
139
    }
 
140
  }
 
141
 
 
142
  // increment time, which can be negative
 
143
  time += rate;
 
144
 
 
145
  return finished;
 
146
}