001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030/** 031 * 032 */ 033package org.openimaj.audio.samples; 034 035import gnu.trove.iterator.TFloatIterator; 036import gnu.trove.list.array.TFloatArrayList; 037 038import java.util.Iterator; 039 040import org.openimaj.audio.AudioFormat; 041import org.openimaj.audio.SampleChunk; 042import org.openimaj.audio.timecode.AudioTimecode; 043import org.openimaj.util.array.ArrayUtils; 044 045/** 046 * An implementation of a sample buffer that maintains the floating point 047 * precision values. Note that this buffer has no timecode associated with it 048 * and that {@link SampleChunk}s cannot be retrieved from it. 049 * 050 * @author David Dupplaw (dpd@ecs.soton.ac.uk) 051 * @created 27 Jul 2012 052 * @version $Author$, $Revision$, $Date$ 053 */ 054public class FloatSampleBuffer implements SampleBuffer, Iterator<Float> 055{ 056 /** The samples */ 057 private float[] samples = null; 058 059 /** The audio format */ 060 private AudioFormat format = null; 061 062 /** Iterator over the samples in this buffer */ 063 private TFloatIterator tfIterator; 064 065 /** 066 * @param samples 067 * The samples to use 068 * @param af 069 * The audio format of the samples 070 */ 071 public FloatSampleBuffer(final float[] samples, final AudioFormat af) { 072 this.format = af.clone(); 073 this.format.setNBits(-1); 074 this.samples = samples; 075 } 076 077 /** 078 * 079 * @param samples 080 * The samples to use 081 * @param af 082 * The audio format 083 */ 084 public FloatSampleBuffer(final double[] samples, final AudioFormat af) { 085 this(ArrayUtils.convertToFloat(samples), af); 086 } 087 088 /** 089 * {@inheritDoc} 090 * 091 * @see org.openimaj.audio.samples.SampleBuffer#get(int) 092 */ 093 @Override 094 public float get(final int index) { 095 return this.samples[index]; 096 } 097 098 /** 099 * {@inheritDoc} 100 * 101 * @see org.openimaj.audio.samples.SampleBuffer#set(int, float) 102 */ 103 @Override 104 public void set(final int index, final float sample) { 105 this.samples[index] = sample; 106 } 107 108 /** 109 * {@inheritDoc} 110 * 111 * @see org.openimaj.audio.samples.SampleBuffer#size() 112 */ 113 @Override 114 public int size() { 115 return this.samples.length; 116 } 117 118 /** 119 * {@inheritDoc} 120 * 121 * @see org.openimaj.audio.samples.SampleBuffer#getFormat() 122 */ 123 @Override 124 public AudioFormat getFormat() { 125 return this.format; 126 } 127 128 /** 129 * {@inheritDoc} 130 * 131 * @see org.openimaj.audio.samples.SampleBuffer#setFormat(org.openimaj.audio.AudioFormat) 132 */ 133 @Override 134 public void setFormat(final AudioFormat af) { 135 this.format = af.clone(); 136 this.format.setNBits(-1); 137 } 138 139 /** 140 * Returns NULL. If you need a sample chunk from this sample buffer, then 141 * you must instantiate the appropriate sample chunk first and fill it. It 142 * cannot be done from this class because this class no longer knows how 143 * many bits you would like the sample chunk to be created as. 144 * 145 * {@inheritDoc} 146 * 147 * @see org.openimaj.audio.samples.SampleBuffer#getSampleChunk() 148 */ 149 @Override 150 public SampleChunk getSampleChunk() { 151 return null; 152 } 153 154 /** 155 * Returns NULL. If you need a sample chunk from this sample buffer, then 156 * you must instantiate the appropriate sample chunk first and fill it. It 157 * cannot be done from this class because this class no longer knows how 158 * many bits you would like the sample chunk to be created as. 159 * 160 * {@inheritDoc} 161 * 162 * @see org.openimaj.audio.samples.SampleBuffer#getSampleChunk(int) 163 */ 164 @Override 165 public SampleChunk getSampleChunk(final int channel) { 166 return null; 167 } 168 169 /** 170 * {@inheritDoc} 171 * 172 * @see org.openimaj.audio.samples.SampleBuffer#asDoubleArray() 173 */ 174 @Override 175 public double[] asDoubleArray() { 176 return ArrayUtils.convertToDouble(this.samples); 177 } 178 179 /** 180 * {@inheritDoc} 181 * 182 * @see org.openimaj.audio.samples.SampleBuffer#asDoubleChannelArray() 183 */ 184 @Override 185 public double[][] asDoubleChannelArray() 186 { 187 final int nc = this.format.getNumChannels(); 188 final double[][] s = new double[nc][this.samples.length / nc]; 189 for (int c = 0; c < nc; c++) 190 for (int sa = 0; sa < this.samples.length / nc; sa++) 191 s[c][sa] = this.samples[sa * nc + c]; 192 return s; 193 } 194 195 /** 196 * {@inheritDoc} 197 * 198 * @see org.openimaj.audio.samples.SampleBuffer#getUnscaled(int) 199 */ 200 @Override 201 public float getUnscaled(final int index) { 202 return this.get(index); 203 } 204 205 /** 206 * Multipy the samples by the given scalar 207 * 208 * @param scalar 209 * The scalar 210 * @return this object 211 */ 212 public FloatSampleBuffer multiply(final double scalar) { 213 for (int i = 0; i < this.samples.length; i++) 214 this.set(i, (float) (this.samples[i] * scalar)); 215 return this; 216 } 217 218 /** 219 * Add the scalar to all the samples 220 * 221 * @param scalar 222 * The scalar 223 * @return this object 224 */ 225 public FloatSampleBuffer add(final double scalar) { 226 for (int i = 0; i < this.samples.length; i++) 227 this.set(i, (float) (this.samples[i] + scalar)); 228 return this; 229 } 230 231 /** 232 * {@inheritDoc} 233 * 234 * @see java.lang.Iterable#iterator() 235 */ 236 @Override 237 public Iterator<Float> iterator() 238 { 239 this.tfIterator = this.tf_iterator(); 240 return this; 241 } 242 243 /** 244 * Returns a trove float iterator 245 * 246 * @return a trove float iterator 247 */ 248 public TFloatIterator tf_iterator() 249 { 250 final TFloatArrayList l = new TFloatArrayList(); 251 for (final float f : this.samples) 252 l.add(f); 253 return l.iterator(); 254 } 255 256 /** 257 * {@inheritDoc} 258 * 259 * @see java.util.Iterator#hasNext() 260 */ 261 @Override 262 public boolean hasNext() 263 { 264 return this.tfIterator.hasNext(); 265 } 266 267 /** 268 * {@inheritDoc} 269 * 270 * @see java.util.Iterator#next() 271 */ 272 @Override 273 public Float next() 274 { 275 return this.tfIterator.next(); 276 } 277 278 /** 279 * {@inheritDoc} 280 * 281 * @see java.util.Iterator#remove() 282 */ 283 @Override 284 public void remove() 285 { 286 this.tfIterator.remove(); 287 } 288 289 /** 290 * {@inheritDoc} 291 * 292 * @see org.openimaj.audio.samples.SampleBuffer#getStartTimecode() 293 */ 294 @Override 295 public AudioTimecode getStartTimecode() 296 { 297 return null; 298 } 299}