Skip to content
This repository has been archived by the owner on Feb 11, 2022. It is now read-only.

TruncationTest

cardin edited this page Jun 22, 2011 · 3 revisions

This guide is based on the MP3GaplessTest demo that's in \test\BasicTest\src\TruncationTest.as
The compiled application can be found at http://cardin.github.com/SeiON/v0.3.0/demos/TruncationTest.html

Aim of Demo:

  • See how a truncated SeionClip is created
  • Understand how truncation affects SeionClip.progress

Useful Sources:


Creating our Sound Objects and SeionGroup

In this demo, we'll be creating 3 sound loops based off a single sound source by manipulating their offset and truncate points.

We start off by embedding the MP3 we want into a Sound object. Since we're creating 3 different sound loops, we need 3 SeionClip variables to contain each loop. Correspondingly, we also need a SeionGroup with 3 allocations so that we can play all 3 sounds simultaneously.

		// Retrieved from http://www.jazzhouseblues.com/
		[Embed(source='../lib/Muffin Man Swing.mp3')]
		private var snd_class:Class;
		private var snd:Sound = new snd_class() as Sound;

		// The SeionGroup to hold our SeionClips
		private var sndGrp:SeionGroup;
		private var sndClip1:SeionClip, sndClip2:SeionClip, sndClip3:SeionClip;
		// ...
		sndGrp = Seion.createSeionGroup("", 3);

Since each sound loop will be played differently, we'll also need to give them each their own pair of truncate and offset variables:

		// 3 diff properties to be applied
		private var offset1:uint, truncate1:uint;
		private var offset2:uint, truncate2:uint;
		private var offset3:uint, truncate3:uint;

Note

I should probably mention that the code in this demo will not be pretty. In the sense that instead of referring to hard-coded variables, we will see a lot of dynamic references such as var sc:SeionClip = this["sndClip" + choice];. This is to avoid repetitive coding in the demo, as we have 3 v.similar but unique objects to manipulate. By passing around just their ID number, we can easily make a dynamic reference to the sound objects and their data variables.

Getting Offset and Truncate

In this demo we are using a custom UI component to help us visualise our offset and truncation points. We don't normally touch on rendering code, but the data values are just shown here to get a feel of what kind of values are being fed in SeionClip later. The slider's values range from 0.0 to 1.0:

			slider1 = new RangeProgressSlider(...); // the visualiser
			
			slider1.lRange = 0.03;		slider1.rRange = 0.1;
			slider2.lRange = 0.21;		slider2.rRange = 0.325;
			slider3.lRange = 0.43;		slider3.rRange = 0.68;

Using a event handler, we pipe the slider's values into the appropriate SeionClip's offset and truncate data variables (eg. offset1 and truncate1). SeionClip accepts the values in MilliSeconds only, so we multiply the value with the full length as defined in SeionClip.length.

		private function updateSndProp(choice:uint):void
		{
			this["offset" + choice] = this["slider" + choice].lRange * snd.length;
			this["truncate" + choice] = (1 - this["slider" + choice].rRange) * snd.length;
		}

With that, we have the values we want and are ready to create the SeionClips for playing.

Playing Excerpts

To create a truncated (read: shortened) SeionClip, we use the static factory method SeionClip.createExcerpt. It works similarly to SeionClip.create(...); in fact SeionClip.create(...) actually pipes its calls to createExcerpt(...). So basically, here we create a SeionClip excerpt of undefined name, managed by the SeionGroup sndGrp, is based on Sound snd, has unlimited looping, is not an autodisposable sound, does not specify a SoundTransform object, and has offset and truncate:

			this["sndClip" + choice] = SeionClip.createExcerpt("", sndGrp, snd, -1, false, null,
												this["offset" + choice], this["truncate" + choice]);
			SeionClip(this["sndClip" + choice]).play();

The reason why we delayed the instantiation of the SeionClip is because the offset and truncate values are immutable - they cannot be changed after instantiation. If they had been mutable, it would not do very well when you change the value during playback.

As always, when we are done with the looping, we properly dispose of the clip and remove any references to it from our code:

			this["sndClip" + choice].dispose();
			this["sndClip" + choice] = null;

The value of SeionClip.progress

With all this truncating and offsetting, you must be wondering what would be the value of the time-keeping methods of SeionClip, such as SeionClip.position, SeionClip.progress and SeionClip.length. The answer is that they will be giving the truncated values now.

For example, assume a Sound of duration 10 seconds, inserted into a SeionClip of offset 2 seconds and truncation 1 second. The playhead is currently paused at the 5 second mark of the original 10 seconds.

Below are its statistics:

  • Length : 10 - 2 [offset] - 1 [truncate] = 7 seconds
  • Position : 5 - 2 [offset] = 3 seconds
  • Progress : 3 / 7 = 0.427.. (and counting)