コード例 #1
0
        public ApplicationSprite()
        {
            // well does the assets library do something with mp3 for us?
            //jeepengine

            // not for AIR.
            // lets move into embedded resources

            var t = new TextField
            {
                multiline = true,

                autoSize = TextFieldAutoSize.LEFT,

                text = "click to start",

                x = 100

                // X:\jsc.svn\examples\actionscript\Test\TestWorkerConsole\TestWorkerConsole\ApplicationSprite.cs
            }.AttachToSprite().AsConsole();

            new net.hires.debug.Stats().AttachToSprite();


            // X:\jsc.svn\examples\actionscript\air\AIRThreadedSoundAsyncLoop\AIRThreadedSoundAsyncLoop\ApplicationSprite.cs

            var threadId = 0;

            t.click += delegate
            {
                threadId++;

                Console.WriteLine(new { threadId });

                var tt = new Thread(async arg0 =>
                {
                    // https://www.packtpub.com/books/content/flash-development-android-audio-input-microphone

                    Console.WriteLine("enter thread");

                    var SourceAudio = KnownEmbeddedResources.Default[
                        "assets/TestWorkerSoundAssetLoop/jeepengine.mp3"
                    ].ToSoundAsset();

                    // sometimes wont reach here, why? timerace issue?
                    Console.WriteLine("SourceAudio " + new { SourceAudio.bytesTotal, SourceAudio.id3 });

                    var SourceAudioBytes = new ByteArray { endian = Endian.LITTLE_ENDIAN };


                    //2647ms SourceAudio { { bytesTotal = 34625, id3 = [object ID3Info] } }
                    //2669ms SourceAudio { { samplesperchannel = 105984 } }


                    var samplesperchannel = (int)SourceAudio.extract(
                        target: SourceAudioBytes,
                        length: 0x100000,
                        startPosition: 0
                    );

                    //var MAGIC_DELAY = 2257u; // LAME 3.98.2 + flash.media.Sound Delay
                    SourceAudioBytes.position = 0;

                    // cyclical binary reader?


                    //                    63ms enter thread
                    //66ms SourceAudio { { bytesTotal = 34625, id3 = [object ID3Info] } }
                    //                    95ms SourceAudio { { samplesperchannel = 105984 } }


                    Console.WriteLine("SourceAudio " + new { samplesperchannel });

                    // can we await for a click here?
                    // what if the parameter is the onclick task? /event

                    // should we prerender our audio loop into the pitch we would need?
                    //var loopjeep = new Abstractatech.ActionScript.Audio.MP3PitchLoop(SourceAudio);

                    //it works and keeps the fps
                    // on android it sounds choppy. why?
                    // nexus seems to be able to do 16sounds with 60fps.
                    //loopjeep.Sound.play();

                    var s = new Sound();

                    while (true)
                    {
                        // X:\jsc.svn\examples\actionscript\Test\TestWorkerSoundAssetLoop\TestWorkerSoundAssetLoop\ApplicationSprite.cs

                        var e = await s.async.sampleData;

                        // ftt


                        //1831ms { { position = 0 } }
                        //1847ms { { position = 65536 } }


                        // wrap
                        if (8192 * 8 + SourceAudioBytes.position > SourceAudioBytes.length)
                        {
                            Console.WriteLine(new { SourceAudioBytes.position });

                            SourceAudioBytes.position = 0;
                        }


                        // can we get the pitch from another device over lan?
                        // can we have framerate as audio?
                        for (var c = 0; c < 8192; c++)
                        {
                            // mipmap?
                            // 4
                            var q0 = SourceAudioBytes.readFloat();
                            // 4
                            var q1 = SourceAudioBytes.readFloat();



                            // i wonder, can we use the orientation
                            // or magnetic north here?
                            // prep for Gear VR?
                            e.data.writeFloat(q0 * 0.7);
                            e.data.writeFloat(q1 * 0.7);
                        }
                    }

                }
                );

                tt.Start(null);
            };

            // can we get heatzeeker to be like a small earth ball?


        }
コード例 #2
0
        private Sound f(Sound _mp3)
        {
            var _target = new ByteArray();

            var _position = 0.0;

            var _sound = new Sound();

            _sound.sampleData +=
                e =>
                {
                    //-- REUSE INSTEAD OF RECREATION
                    _target.position = 0;

                    //-- SHORTCUT
                    var data = e.data;

                    var scaledBlockSize = BLOCK_SIZE * _rate;
                    var positionInt = Convert.ToInt32(_position);
                    var alpha = _position - positionInt;

                    var positionTargetNum = alpha;
                    var positionTargetInt = -1;

                    //-- COMPUTE NUMBER OF SAMPLES NEED TO PROCESS BLOCK (+2 FOR INTERPOLATION)
                    var need = Convert.ToInt32(Math.Ceiling(scaledBlockSize) + 2);

                    //-- EXTRACT SAMPLES
                    var read = (int)_mp3.extract(_target, need, positionInt);

                    var n = BLOCK_SIZE;
                    if (read != need)
                        n = Convert.ToInt32(read / _rate);

                    var l0 = .0;
                    var r0 = .0;
                    var l1 = .0;
                    var r1 = .0;

                    var i = 0;
                    for (; i < n; i++)
                    {
                        //-- AVOID READING EQUAL SAMPLES, IF RATE < 1.0
                        if (Convert.ToInt32(positionTargetNum) != positionTargetInt)
                        {
                            positionTargetInt = Convert.ToInt32(positionTargetNum);

                            //-- SET TARGET READ POSITION
                            _target.position = (uint)(positionTargetInt << 3);

                            //-- READ TWO STEREO SAMPLES FOR LINEAR INTERPOLATION
                            l0 = _target.readFloat();
                            r0 = _target.readFloat();

                            l1 = _target.readFloat();
                            r1 = _target.readFloat();
                        }

                        //-- WRITE INTERPOLATED AMPLITUDES INTO STREAM
                        data.writeFloat(l0 + alpha * (l1 - l0));
                        data.writeFloat(r0 + alpha * (r1 - r0));

                        //-- INCREASE TARGET POSITION
                        positionTargetNum += _rate;

                        //-- INCREASE FRACTION AND CLAMP BETWEEN 0 AND 1
                        alpha += _rate;
                        while (alpha >= 1.0) --alpha;
                    }

                    //-- FILL REST OF STREAM WITH ZEROs
                    if (i < BLOCK_SIZE)
                    {
                        while (i < BLOCK_SIZE)
                        {
                            data.writeFloat(0.0);
                            data.writeFloat(0.0);

                            ++i;
                        }
                    }

                    //-- INCREASE SOUND POSITION
                    _position += scaledBlockSize;
                };


            return _sound;

        }