HappyDog
HappyDog

Reputation: 1351

SuperCollider: How to process a log file in real time

I'd like to use SuperCollider to generate programmatic music based on output from a log file, generated on the same server.

Programs will write to the log file and my sclang script will respond to the lines, in real-time, as they are written. The code will modify the sonic output based on type and frequency of the log entries.

I have a script that can read a log file and output sounds based on the contents, but I haven't found a way to adapt to it as it is written.

I am imagining that the logic would be something like this:

Is there a way to do something of this nature in an efficient manner whilst avoiding locking/race issues (e.g. reading a line that is only half-written) and without locking-up the system in a tight loop?

If it can't be done natively, I'd consider a solution that some external script (e.g. in Python/Perl/whatever) could process the file and forward new entries as they become available, but I'm not sure how this would work in practice, either.

Suggestions for either type of solution are welcome.

EDIT: The log entries are either (a) triggering one-off samples; or (b) modifying parameters that affect the ongoing algorithmic generation of audio. The sclang script would be generating music even when no new log entries are received, so any loop-based approach would need to yield (or whatever you might call it) to allow the generated audio to continue.

Upvotes: 0

Views: 104

Answers (1)

les_h
les_h

Reputation: 401

I got this to work

(
var f, length;
f = File("~/foo.txt".standardizePath,"r");
length = f.length;
{
    inf.do({
        1.wait;
        f = File("~/foo.txt".standardizePath,"r");
        f.pos = length;
        length = f.length; 

        1.wait;
        f.getLine(1024).postln;
        f.close;
    });
}.fork

)

f.seek(0,2); which should have put my cursor at the end of the file did not work for me.

Depending on how the log file is being generated, you might look at whether it would make more sense to generate OSC messages rather than writing to a file.

Upvotes: 0

Related Questions