001: /*
002: * Copyright (c) 2000 Silvere Martin-Michiellot All Rights Reserved.
003: *
004: * Silvere Martin-Michiellot grants you ("Licensee") a non-exclusive,
005: * royalty free, license to use, modify and redistribute this
006: * software in source and binary code form,
007: * provided that i) this copyright notice and license appear on all copies of
008: * the software; and ii) Licensee does not utilize the software in a manner
009: * which is disparaging to Silvere Martin-Michiellot.
010: *
011: * This software is provided "AS IS," without a warranty of any kind. ALL
012: * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
013: * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
014: * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. Silvere Martin-Michiellot
015: * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
016: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
017: * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
018: * Silvere Martin-Michiellot OR ITS LICENSORS BE LIABLE
019: * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
020: * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
021: * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
022: * OR INABILITY TO USE SOFTWARE, EVEN IF Silvere Martin-Michiellot HAS BEEN
023: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
024: *
025: * This software is not designed or intended for use in on-line control of
026: * aircraft, air traffic, aircraft navigation or aircraft communications; or in
027: * the design, construction, operation or maintenance of any nuclear
028: * facility. Licensee represents and warrants that it will not use or
029: * redistribute the Software for such purposes.
030: *
031: */
032:
033: // This code is repackaged after the code from Craig A. Lindley, from Digital Audio with Java
034: // Site ftp://ftp.prenhall.com/pub/ptr/professional_computer_science.w-022/digital_audio/
035: // Email
036: package com.db.media.audio.dsp.sources;
037:
038: // NOTE: 16 bit PCM data has a min value of -32768 (8000H)
039: // and a max value of 32767 (7FFFFH).
040:
041: public class StereoOscillator extends Oscillator {
042:
043: // Private class data
044: private int frequencyL;
045: private int frequencyR;
046: private int posL;
047: private int posR;
048: private boolean toggle;
049: private double leftAmplitudeAdj;
050: private double rightAmplitudeAdj;
051:
052: public StereoOscillator(int type, int frequencyL, int frequencyR,
053: int sampleRate, NegotiationListener negComplete) {
054:
055: super (type, 0, sampleRate, 2, negComplete);
056:
057: // Save incoming
058: this .frequencyL = frequencyL;
059: this .frequencyR = frequencyR;
060:
061: toggle = false;
062: posL = posR = 0;
063:
064: leftAmplitudeAdj = 1.0;
065: rightAmplitudeAdj = 1.0;
066:
067: }
068:
069: // Constructor with reasonable defaults
070: public StereoOscillator(NegotiationListener negComplete) {
071:
072: this (SINEWAVE, 440, 880, 22050, negComplete);
073:
074: }
075:
076: public int getSamples(short[] buffer, int length) {
077:
078: int sample = 0;
079: int count = length;
080:
081: while (count-- != 0) {
082:
083: if (!toggle) {
084:
085: toggle = true;
086:
087: buffer[sample++] = (short) (leftAmplitudeAdj * waveTable[posL]);
088:
089: posL += frequencyL;
090: if (posL >= sampleRate)
091: posL -= sampleRate;
092:
093: } else {
094:
095: toggle = false;
096:
097: buffer[sample++] = (short) (rightAmplitudeAdj * waveTable[posR]);
098:
099: posR += frequencyR;
100: if (posR >= sampleRate)
101: posR -= sampleRate;
102: }
103: }
104: return length;
105:
106: }
107:
108: public int getLeftFrequency() {
109:
110: return frequencyL;
111:
112: }
113:
114: public void setLeftFrequency(int frequency) {
115:
116: this .frequencyL = frequency;
117:
118: // Reset waveTable index
119: posL = 0;
120:
121: }
122:
123: public int getRightFrequency() {
124:
125: return frequencyR;
126:
127: }
128:
129: public void setRightFrequency(int frequency) {
130:
131: this .frequencyR = frequency;
132:
133: // Reset waveTable index
134: posR = 0;
135:
136: }
137:
138: public double getLeftAmplitudeAdj() {
139:
140: return leftAmplitudeAdj;
141:
142: }
143:
144: public void setLeftAmplitudeAdj(double amplitudeAdj) {
145:
146: this .leftAmplitudeAdj = amplitudeAdj;
147:
148: }
149:
150: public double getRightAmplitudeAdj() {
151:
152: return rightAmplitudeAdj;
153:
154: }
155:
156: public void setRightAmplitudeAdj(double amplitudeAdj) {
157:
158: this.rightAmplitudeAdj = amplitudeAdj;
159:
160: }
161:
162: }
|