Stuttering audio with CsoundPerformanceThread

I’ve run into a stuttering audio issue on my MacBook Pro on Catalina. Here’s the scenario. I’m generating a Csound score, sending the full CSD file to an instance of Csound via ctcsound, and then playing and pausing the performance with CsoundPerformanceThread.

The stuttering audio occurs whenever the system’s sound output device is set to “MacBook Pro Speakers” or “External Headphones”. The stuttering goes away whenever the system’s sound output device is set to my “Scarlett 2i2 USB” audio interface or my docking station’s “USB audio CODEC”. Here are audio examples of the stuttering and non-stuttering.

I’ve never run into this stuttering issue before with my Csound projects, but this is my first time working with the CsoundPerformanceThread. Have any of you had experience with this stuttering issue? Could you give me guidance on how to fix this?

Thanks!
Jason

not sure it is related, but did you see françois’ explanation here?

Thanks, Joachim. I believe I am following Francois’ instructions from that notebook. Here’s a simplified version of my Python code. The while True: block is the GUI event loop. I hit “Generate” to produce a Csound score, load it into the Csound instance with .compileCsdText(), .start() the Csound instance, and create a CsoundPerformanceThread. Hitting “Play” and “Pause” then triggers the corresponding methods of CsoundPerformanceThread.

The thing that confuses me is why this program works perfectly fine on some output devices but stutters on the other output devices on my Mac.

while True:
        event, values = window.read(timeout=100)
        if event == sg.WIN_CLOSED or event == 'Exit':
            break

        if event == "Generate":
            csd = generate_score(values, cs)
            pt = ctcsound.CsoundPerformanceThread(cs.csound())
        if event == "Play":
            pt.play()
        if event == "Pause":
            pt.pause()
        if event == "Stop":
            pt.stop()
            pt.join()
            cs.reset()
        if event == "Rec":
            pt.record("test.wav",44100, 2)
        if event == "StopRec":
            pt.stopRecord()

window.close()


def generate_score(values, cs):
    piece = Piece(melody_voices=values["-MELODY-"], drum_voices=values["-DRUMS-"], bass_voices=values["-BASS-"], tempo=values["-TEMPO-"], swing=values["-SWING-"])
    score = piece.perform() # NOTE, different than the Csound.perform() function

    with open("orchestra.orc","r") as csnd_orchestra:
        csd = ""
        for line in csnd_orchestra:
            csd += line

    csd = csd + score + '''

    s
    i 1 0 1 0 0 0
    i -1000 2 0

    </CsScore>
    </CsoundSynthesizer>
    '''
    ret = cs.compileCsdText(csd)
    if ret == ctcsound.CSOUND_SUCCESS:
        cs.start()
    return csd

Could this be a CoreAudio issue? I’m reading about some complaints with Apple’s CoreAudio. Do you think it’d be worth exploring JACK? I’ve never used JACK, but I’m open to anything that’ll fix this stuttering issue.

Adding -+rtaudio=coreaudio in the CsOptions section of the CSD solved the problem. The Pause button now works cleanly without audio stutter.

Jason

Thanks for posting the solution. The only time I use Python with Csound is for generating graphs, no real time output at all. Although my knowledge of Python is limited I found ctcsound to be very well put together. Good to hear that it’s working fine for you too.