0

Hey I'm stuck on this small Spotify app I'm putting together to help practice new songs. It simply grabs a start time and end time and then once activated, continues to loop over it - good for learning difficult sections in a track.

Problem: I can see from a console log that the RealtimeAnalyzer is putting between 5 and 11 console logs of my message as well as the .seek function I'm calling - so when the current position is greater than the end point, it's using seek to go back to the In Point but is stuttering.

How can I break out of this call so I can prevent this stuttering?

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="$views/css/image.css">
        <link rel="stylesheet" href="$views/css/list.css">
        <link rel="stylesheet" href="$views/css/buttons.css">
        <link rel="stylesheet" href="css/main.css">
        <link rel="stylesheet" href="css/github.css">
    </head>
    <body>
        <div id="wrapper">
            <div id="index" class="section">
                    <h1>Loop Sections of Songs for Training</h1>


                <table id="mytable" width="50%" border="1" cellspacing="0" cellpadding="2">
                    <tbody>
                        <tr>
                            <th>
                                In Point
                            </th>
                            <th>
                                Out Point
                            </th>
                            <th colspan="3">
                                Actions
                            </th>
                        </tr>
                        <tr id="row0" class="row">
                            <td>
                                <button class="inPoint" id="inPoint0">0:00</button>
                            </td>
                            <td>
                                <button class="outPoint" id="outPoint0">0:00</button>
                            </td>
                            <td>
                                <button class="startLoop" id="startLoop0">Loop</button>
                            </td>
                            <td>
                                <button class="stopLoop" id="stopLoop0">Stop Loop</button>
                            </td>
                            <td>
                                <button class="clearLoop" id="clearLoop0">Clear Loop</button>
                            </td>
                        </tr>
                    </tbody>
                </table>
                <button id="add">+</button>

            </div>
        </div>
         <script src="/js/jquery.min.js"></script>



    <script type="text/javascript" data-container="js">
    require(['$api/audio', '$api/models'], function(audio, models) {

        var analyzer = audio.RealtimeAnalyzer.forPlayer(models.player);
        var outPointValue = 0;
        var inPointValue = 0;
        var inPoint = $('.inPoint');
        var outPoint = $('.outPoint');
        var clearLoop = $('.clearLoop');
        var startLoop = $('.startLoop');
        var stopLoop = $('.stopLoop');
        var trackPosition = null;
        var loop = false;


        //Watch Track Player Position
        analyzer.addEventListener('audio', monitorAudio);

        //Get The Current Player Position
        function monitorAudio() {

            models.player.load('position').done(function(p) {
                console.log(p.position);

                if(loop && p.position >= outPointValue && outPointValue !== 0) {
                    console.log('Begin to Loop Section Again');
                    p.seek(inPointValue);
                }

                if(inPoint.data('inClicked')) {
                    console.log('Set inPoint');
                    inPointValue = p.position;
                    inPoint.html(msToTime(inPointValue));   
                    inPoint.data('inClicked', false);                     
                }

                if(outPoint.data('outClicked')) {
                    console.log('Set outPoint');
                    outPointValue = p.position;
                    outPoint.html(msToTime(outPointValue));
                    outPoint.data('outClicked', false);
                }

            });

        }

        stopLoop.prop('disabled', true);

        startLoop.on('click', function() {
            loop = true;
            $(this).prop('disabled', true);
            //var trid = $(this).closest('tr').attr('id');
            $('.stopLoop').prop('disabled', false);
        });

        stopLoop.on('click', function() {
            loop = false;
            //console.log(this.id);
            $(this.id).prop('disabled', false);
            $(this.id).closest('.stopLoop').prev().prop('disabled', true);
        });

        clearLoop.on('click', function() {
            loop = false;
            inPointValue = 0;
            inPoint.html('0:00');
            outPointValue = 0;
            outPoint.html('0:00');
        });

        inPoint.click(function(){
            $(this).data('inClicked', true);
        });

        outPoint.click(function(){
            $(this).data('outClicked', true);
        });

        function msToTime(s) {
          var ms = s % 1000;
          s = (s - ms) / 1000;
          var secs = s % 60;
          s = (s - secs) / 60;
          var mins = s % 60;
          var hrs = (s - mins) / 60;

          return mins + ':' + secs;
        }

        $(document).ready(function() {

            var counter = 1;

            $("#add").click(function() {

                $('#mytable .row:last').clone(true).insertAfter('#mytable .row:last');

                $('#mytable .row:last').attr('id', 'row' + counter);
                $('#mytable .row:last button').each(function() {

                    $(this).attr('id', $(this).attr('class') + counter);
                });

                counter++;

                return false;
            });
        });

    });

    </script>

    </body>
</html>
Thomas
  • 3,348
  • 4
  • 35
  • 49
Mick Davies
  • 93
  • 1
  • 2
  • 11

1 Answers1

0

I'd say you should be able to add a var requested_seek=false, set it to true when you call seek, then set it false again in an else if p.position < outPointValue or something like that. Oh, and if(loop && !requested_seek && p.position >= outPointValue && outPointValue !== 0). Let me know if you need me to work a full sample out, but I think that should enough, given your work so far.

Love the app idea, btw.

Thomas
  • 3,348
  • 4
  • 35
  • 49
  • Thanks Thomas I'll give it a shot and see how it goes. I originally had a setInterval working but then found this in the api. Pretty much being made so I can learn parts of TesseracT drum beats... – Mick Davies Jan 01 '14 at 23:38