Mercurial > hg > jslab
annotate src/samer/units/Wavetable.java @ 8:5e3cbbf173aa tip
Reorganise some more
author | samer |
---|---|
date | Fri, 05 Apr 2019 22:41:58 +0100 |
parents | bf79fb79ee13 |
children |
rev | line source |
---|---|
samer@0 | 1 /* |
samer@0 | 2 * Copyright (c) 2000, Samer Abdallah, King's College London. |
samer@0 | 3 * All rights reserved. |
samer@0 | 4 * |
samer@0 | 5 * This software is provided AS iS and WITHOUT ANY WARRANTY; |
samer@0 | 6 * without even the implied warranty of MERCHANTABILITY or |
samer@0 | 7 * FITNESS FOR A PARTICULAR PURPOSE. |
samer@0 | 8 */ |
samer@0 | 9 |
samer@0 | 10 package samer.units; |
samer@0 | 11 import samer.core.*; |
samer@0 | 12 import samer.maths.*; |
samer@0 | 13 import samer.maths.random.*; |
samer@0 | 14 |
samer@0 | 15 /** |
samer@0 | 16 Periodic generator: generates periodic signal from |
samer@0 | 17 a wave table |
samer@0 | 18 */ |
samer@0 | 19 |
samer@0 | 20 public class Wavetable implements Generator |
samer@0 | 21 { |
samer@0 | 22 int start, end, j; |
samer@0 | 23 Vec vec; |
samer@0 | 24 double [] pp; |
samer@0 | 25 |
samer@0 | 26 public Wavetable(Vec vec) { |
samer@0 | 27 this(vec,0,vec.size()); |
samer@0 | 28 } |
samer@0 | 29 |
samer@0 | 30 public Wavetable(Vec vec, int start, int end) |
samer@0 | 31 { |
samer@0 | 32 this.vec = vec; |
samer@0 | 33 this.start = start; |
samer@0 | 34 this.end = end; |
samer@0 | 35 pp = vec.array(); |
samer@0 | 36 j = start; |
samer@0 | 37 } |
samer@0 | 38 |
samer@0 | 39 public int period() { return end-start; } |
samer@0 | 40 public void dispose() {} |
samer@0 | 41 public double next() |
samer@0 | 42 { |
samer@0 | 43 double r=pp[j++]; |
samer@0 | 44 if (j>=end) j=start; |
samer@0 | 45 return r; |
samer@0 | 46 } |
samer@0 | 47 |
samer@0 | 48 public void next(double [] x) |
samer@0 | 49 { |
samer@0 | 50 int remaining=x.length; |
samer@0 | 51 int iput=0; |
samer@0 | 52 |
samer@0 | 53 while (remaining>0) { |
samer@0 | 54 int avail = end-j; |
samer@0 | 55 int block = remaining > avail ? avail : remaining; |
samer@0 | 56 |
samer@0 | 57 System.arraycopy( pp, j, x,iput, block); |
samer@0 | 58 j+=block; iput+=block; remaining-=block; |
samer@0 | 59 if (j>=end) j=start; |
samer@0 | 60 } |
samer@0 | 61 } |
samer@0 | 62 |
samer@0 | 63 public String toString() { return "wavetable"; } |
samer@0 | 64 } |
samer@0 | 65 |
samer@0 | 66 |