Skip to content

Instantly share code, notes, and snippets.

@reprimande
Created December 8, 2012 09:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save reprimande/4239620 to your computer and use it in GitHub Desktop.
Save reprimande/4239620 to your computer and use it in GitHub Desktop.
play binary by supercollider
(
s.waitForBoot {
var rootPath, window, view,
drawBuf, bufnum, sound, task,
width, height, fps, curX, curY, pixSize,
processFunc, loadFile;
// initial settings
rootPath = "/path/to/root/dir";
pixSize = 5;
fps = 30;
width = 480; height= 640;
curX = 0; curY = 0;
// create synthdef
SynthDef(\bufplayer, {|bufnum=0, pan=0, amp=1|
var out = PlayBuf.ar(1, bufnum, BufRateScale.kr(bufnum), loop:1);
Out.ar(0, Pan2.ar(out, pan, amp));
}).add;
s.sync;
// initial sound
bufnum = Buffer.alloc(s, 0);
sound = Synth(\bufplayer, [\bufnum, bufnum]);
// initial window/view
window = Window(rootPath, Rect(99, 99, width, height), true);
view = UserView(window, Rect(0, 0, width, height));
view.resize = 5;
view.background= Color.black;
//view.animate = true;
//view.frameRate = fps;
view.drawFunc = {|uview|
var w = window.bounds.width;
var h = window.bounds.height;
if (drawBuf.size > 0, {
(drawBuf.size / 4).do{|i|
var index, r, g, b, a;
index = i * 4;
r = drawBuf[index];
g = drawBuf[index + 1];
b = drawBuf[index + 2];
a = drawBuf[index + 3];
Pen.fillColor = Color.new(r, g, b, a);
Pen.fillRect(Rect.aboutPoint(Point(curX, curY), pixSize, pixSize));
curX = curX + pixSize;
if (curX > w, {curX = 0; curY = curY + pixSize});
if (curY > h, {curY = 0});
};
});
};
view.clearOnRefresh= false;
window.onClose= {
task.stop;
sound.free;
bufnum.free;
drawBuf.free;
};
window.front;
CmdPeriod.doOnce({if(window.isClosed.not, {window.close})});
// define process function
loadFile = {|path|
var file, size, buffer, bufHex;
file = File.open(path, "rb+");
size = file.length;
buffer = Array.fill(size, 0);
if (size % 1 == 0, {size = size - 1;});
size.do({|i|
var val = file.getInt8;
buffer[i] = val;
});
file.close;
buffer;
};
processFunc = {|path|
var pathName, entries;
pathName = PathName.new(path);
entries = pathName.entries;
while { entries.size > 0 } {
var item, fullPath;
item = entries.removeAt(0);
fullPath = item.fullPath;
window.name = fullPath;
if (File.type(fullPath) == \directory,
{
processFunc.value(fullPath);
}, {
var frameSize, frameCount, fileIntBuf, curPos;
fileIntBuf = loadFile.value(fullPath);
curPos = 0;
frameSize = 44100 / fps;
frameCount = (fileIntBuf.size / frameSize).ceil;
frameCount.do {|i|
var sndBuf;
var bufSize = frameSize;
var remainSize = fileIntBuf.size - (i * frameSize);
if (frameSize > remainSize, { bufSize = remainSize; } );
sndBuf = Array.fill(bufSize, 0);
drawBuf = Array.fill(bufSize, 0);
bufSize.do {|j|
if (fileIntBuf[curPos].notNil, {
var val = fileIntBuf[curPos] / 128;
// create pixel buffer
drawBuf[j] = val.abs;
// create sound buffer
sndBuf[j] = val;
curPos = curPos + 1;
});
};
bufnum.free;
bufnum = Buffer.loadCollection(s, sndBuf);
sound.set(\bufnum, bufnum);
view.refresh;
(1 / fps).wait;
};
}
);
};
};
// execute process
task = Task({ processFunc.value(rootPath) }, AppClock).play;
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment