Skip to content

How to sync audio playback start with my GUI step sequencer #17

@jamesheston

Description

@jamesheston

I'm writing a simple music tracker in Python 3 that uses Csound as its audio engine, and interacts with it through ctcsound.

My main sequencer class is SongSequencer. It has a toggle_playing method that converts the program's phrase and instrument data structures into a string in .csd format. Then I use a CsoundPerformanceThread to play the audio for that in a seperate thread. My problem is that there is delay of a second or two before Csound starts actually playing the audio back, which prevents me from syncing the current step indicator in my GUI to where the audio is in the score. Are there any callback methods I can use to sync my GUI and the audio playback?

I tried using the setRtPlayCallback method but couldn't get it to do anything.

The abbreviated code of my program looks like this:

class SongSequencer:
    def __init__(self):
        #...
        self.cs = ctcsound.Csound()
        self.pt = ctcsound.CsoundPerformanceThread( self.cs.csound() )

    def toggle_playing(self, cursor_step=0):
        if self.pt.status() != 0:
            self.pt.stop()
            self.pt.join()
            self.cs.reset()
            self.pt = ctcsound.CsoundPerformanceThread( self.cs.csound() )
            self.pt.setProcessCB(self.audio_start_cb)

        channels_phrases = [[], [], [], []]
        # Add contiguous filled phrase sequences in current channel, starting at current
        # cursor step. When I find an empty phrase sequence, exit loop
        for ch_i in range(0,4):
            for psq in self.sequence[ch_i][cursor_step:]:
                if not psq:
                    break
                channels_phrases[ch_i].append(psq.phrase.steps)

        csd = phrases_to_csd(self.instruments, channels_phrases)
        ret = self.cs.compileCsdText(csd)
        if ret == ctcsound.CSOUND_SUCCESS:
            self.cs.start()
            self.cs.setPlayOpenCallback(self.audio_start_cb)
            self.pt.play()


    def audio_start_cb(self):
        print('AUDIO PLAYING NOW')
        print('AUDIO PLAYING NOW')
        print('AUDIO PLAYING NOW')

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions