/flash/flash-audio/src/com/ryanberdeen/audio/SoundSampleSource.as

http://echo-nest-remix.googlecode.com/ · ActionScript · 43 lines · 28 code · 6 blank · 9 comment · 2 complexity · 9d3e07a746767f99247e1dc8b127e393 MD5 · raw file

  1. /*
  2. * Copyright 2009 Ryan Berdeen. All rights reserved.
  3. * Distributed under the terms of the MIT License.
  4. * See accompanying file LICENSE.txt
  5. */
  6. package com.ryanberdeen.audio {
  7. import flash.media.Sound;
  8. import flash.utils.ByteArray;
  9. /**
  10. * <code>ISampleSource</code> that provides samples unaltered from a
  11. * <code>Sound</code>.
  12. */
  13. public class SoundSampleSource implements ISampleSource {
  14. private var sound:Sound;
  15. private var startPosition:Number;
  16. private var _length:Number;
  17. public function SoundSampleSource(sound:Sound):void {
  18. this.sound = sound;
  19. startPosition = 0;
  20. _length = Math.ceil(sound.length * 44.1);
  21. }
  22. public function extract(target:ByteArray, length:Number, startPosition:Number = -1):Number {
  23. if (startPosition == -1) {
  24. startPosition = this.startPosition;
  25. }
  26. var result:Number = sound.extract(target, length, startPosition);
  27. this.startPosition = startPosition + length;
  28. return result;
  29. }
  30. public function toSourcePosition(position:Number):Number {
  31. return position;
  32. }
  33. public function get length():Number {
  34. return _length;
  35. }
  36. }
  37. }