Microunds/NRT Question! How to fix this?

I am currently collaborating with AI to enhance my SC3 skills, and so far, the experience has been positive. However, I recognize that some of our peers might have concerns about this approach, especially considering the potential risks of exposing their work to AI systems. This could lead to unintended corporate disclosures, particularly with companies like Ableton, Cycling 74, and Native Instruments, where some individuals might be involved in AI-related music projects, or even with entities like the Volkswagen Group, which is exploring blockchain applications in AI-driven music production.

Despite these concerns, I prefer to be transparent about my use of tools such as ChatGPT, Deepseek, Gemini, and Bing AI. In this context, I’ve drafted a piece of code involving two SynthDefs: one for microsound and another utilizing demand UGens. However, I’m facing challenges in fixing it, and would appreciate any feedback or suggestions on how to resolve the issues.

I am basically trying to:

  • load two synthdefs
  • instantiate them into the score
  • write the demand ugen synth to a file with 8 seconds
  • load that file into a buffer
  • write a new file with the microsounds synthesis definition with 16 seconds
  • open the first file
  • play it
  • close the media player
  • open the second file
  • play it
  • close the media player

Cheers!

// Limpeza inicial
Server.killAll;
Buffer.freeAll;

Server.default.boot;

// **SynthDef 1: Microsound Granular**
(
SynthDef(\roadsMicrosound, {
    |out = 0, bufnum = 0, rate = 1.0, centerPos = 0.5, dur = 0.1, amp = 0.2,
     panSpread = 1.0, density = 50, pitchVar = 0.2|

    var trig = Dust.ar(density);
    var grainDur = LFNoise1.kr(10).range(0.01, 0.06);
    var pos = LFNoise1.kr(15).range(centerPos - 0.1, centerPos + 0.1).clip(0, 1);
    var rateMod = LFNoise1.kr(12).range(1.0 - pitchVar, 1.0 + pitchVar);
    var panPos = LFNoise1.kr(8) * panSpread;

    var snd = GrainBuf.ar(
        numChannels: 2,
        trigger: trig,
        dur: grainDur,
        sndbuf: bufnum,
        rate: rate * rateMod,
        pos: pos,
        interp: 4,
        pan: panPos
    );

    Out.ar(out, snd * amp);  // Saída estéreo
}).add;
)

// **SynthDef 2: Demand Pulse Write**
(
SynthDef(\demandPulseWrite, {
    |out = 0, amp = 0.3, trigFreq = 6|

    var trig = Impulse.ar(trigFreq);
    var dseq = Dseq([0.1, 0.2, 0.05, 0.3, 0.1], inf);
    var dwhite = Dwhite(0.05, 0.25, inf);
    var choose = Demand.ar(trig, 0, Dswitch1([dseq, dwhite], Dseq([0, 1, 0, 1], inf)));

    var sig = Decay2.ar(trig, 0.001, 0.1) * choose;

    Out.ar(out, sig ! 2 * amp);  // Saída estéreo
}).add;
)

(
var inputFilePath = "C:/Users/selfd/Desktop/synth 22042025.scd";
var outputFilePath = "C:/Users/selfd/Desktop/synth_output.aiff";

var completionAction = {
    "Gravação finalizada.".postln;
    "cmd /c start %".format(outputFilePath).unixCmd;
};

if (File.exists(inputFilePath).not) {
    "Arquivo de entrada não existe.".postln;
} {
    Buffer.read(Server.default, inputFilePath, { |buffer|
        if (buffer.isNil) {
            "Erro ao carregar o buffer.".postln;
        } {
            var myScore = Score.new;
            var roadsID = 1000;
            var demandID = 1001;

            myScore.add([
                0.0, [\s_new, \roadsMicrosound, roadsID, 0, 0,
                    \out, 0,
                    \bufnum, buffer.bufnum,
                    \rate, Demand.kr(Impulse.kr(6), 0, Dseq([0.5, 1.0, 0.8, 1.2], inf)),
                    \density, Demand.kr(Impulse.kr(5), 0, Dwhite(0.1, 0.4, inf)),
                    \centerPos, Demand.kr(Impulse.kr(4), 0, Dseq([0.4, 0.5, 0.6], inf))
                ]
            ]);

            myScore.add([
                0.5, [\s_new, \demandPulseWrite, demandID, 0, 0, \out, 0]
            ]);

            // Free demandPulseWrite after 8 seconds (starts at 0.5s → ends at 8.5s)
            myScore.add([
                8.5, [\n_free, demandID]
            ]);

            // Free roadsMicrosound after 16 seconds
            myScore.add([
                16.0, [\n_free, roadsID]
            ]);

            myScore.recordNRT(
                outputFilePath: outputFilePath,
                sampleRate: 48000,
                headerFormat: "AIFF",
                sampleFormat: "int16",
                duration: 17, // slightly more than last free
                action: completionAction
            );

            "Gravação iniciada em: %".format(outputFilePath).postln;
        }
    });
}
)


I am personally reluctant to take a broken AI generated code and fix it for you, because I don’t see how that would help you learn.

Maybe you can share with us what steps you have taken to try to identify and fix your problem on your own?

For example, have you read the help files for Score or the NRT guide, and do you have specific questions about what you have read? Non-Realtime Synthesis (NRT) | SuperCollider 3.12.2 Help

Or, have you made a working real time version of your idea that you have questions about translating to NRT?

4 Likes