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.processors;
037:
038: public class Cache extends AbstractAudio {
039:
040: // Private class data
041: private int cacheSize;
042: private int cacheBufferOffset;
043: private short[] moveBuffer;
044: private short[] cacheBuffer;
045:
046: // Class constructor
047: public Cache() {
048:
049: super ("Cache", AbstractAudio.PROCESSOR);
050:
051: // Indicate the cache is uninitialized
052: cacheSize = 0; // Size of cache required
053: cacheBufferOffset = 0; // Offset to where current samples are
054:
055: }
056:
057: // Process any reset messages received by this stage
058: public void reset() {
059:
060: o("Cache reset");
061:
062: // Reset cache sample offset to the beginning
063: cacheBufferOffset = 0;
064:
065: }
066:
067: // Return samples from the cache
068: public int getSamples(short[] buffer, int length) {
069:
070: // Determine if cache already contains samples
071: if (cacheSize == 0) {
072:
073: // Allocate a move buffer
074: moveBuffer = new short[length];
075:
076: // Cache uninitialized, determine required cache size
077: int len = 0;
078:
079: while (len != -1) {
080: cacheSize += len;
081: len = previous.getSamples(moveBuffer, length);
082: }
083: o("Required cache size in samples: " + cacheSize);
084:
085: // Allocate buffer for cache
086: cacheBuffer = new short[cacheSize];
087:
088: // Now fill the cache buffer samples. NOTE: previous
089: // stages must have reset for this to work.
090:
091: len = previous.getSamples(moveBuffer, length);
092: while (len != -1) {
093:
094: System.arraycopy(moveBuffer, 0, cacheBuffer,
095: cacheBufferOffset, len);
096: cacheBufferOffset += len;
097: len = previous.getSamples(moveBuffer, length);
098: }
099: // Pt at the beginning of the data in the cache
100: cacheBufferOffset = 0;
101: }
102: // Return samples from the cache
103:
104: // Calculate data remaining in the cache
105: int dataRemaining = cacheSize - cacheBufferOffset;
106:
107: // Return end of file indication if no more data
108: if (dataRemaining == 0)
109: return -1;
110:
111: int samplesRead;
112:
113: if (length <= dataRemaining) {
114: // Cache has the required amount of data to return
115: System.arraycopy(cacheBuffer, cacheBufferOffset, buffer, 0,
116: length);
117: cacheBufferOffset += length;
118: return length;
119:
120: } else {
121:
122: // Cache is short of data
123: System.arraycopy(cacheBuffer, cacheBufferOffset, buffer, 0,
124: dataRemaining);
125: cacheBufferOffset += dataRemaining;
126: return dataRemaining;
127: }
128:
129: }
130:
131: }
|