Pseuo UGen for Buffer Loader GUI

Hi all -

I’m attempting to create a simple pseudo-UGen for an abstraction I use pretty regularly, where I like to have access to a Button, File Dialog Box, and a Drag Sink - in order to load files.

I am unsure about how to pass the buffer correctly, though. I was hoping someone here might be able to tell me where the fix is!

Thanks.

FileLoader{
	*new {
		arg window, xPos, yPos, buf, server;
		Button(window, Rect(xPos, yPos, 40, 30))
		.string_("file")
		.action_({|x|
		FileDialog({|paths|
		buf = Buffer.read(server, paths[0]);
		});
	});
	DragSink(window, Rect(xPos+40, yPos, 100, 30)).action_({|x|
	buf = Buffer.read(server, x.value);
});
		buf;
	}
}

If you mean for this to be agnostic about how you use it (as in, all you need it to do is give you Buffer objects that you might use in all sorts of unknown ways) I’d recommend giving it a callback function…

FileLoader{
  *new {
    arg window, xPos, yPos, server, callback;
    
    var action = { |path|
      if (callback.notNil) {
        callback.(Buffer.read(server, path));
      };
    };
    server = server ?? { Server.default };
    
    Button(window, Rect(xPos, yPos, 40, 30))
    .string_("file")
    .action_({|x|
      FileDialog({|paths|
        action.(paths[0]);
      });
    });
    DragSink(window, Rect(xPos+40, yPos, 100, 30)).action_({|x|
      action.(x.value);
    });
  }
}

This way whenever you use it you can tell it what to do with the created Buffer object, e.g.

FileLoader(/*...*/ callback: { |buf| ~buf.free; ~buf = buf };

(Note that this code isn’t actually giving you a FileLoader object, it’s basically a convenience to create two GUI objects with particular actions. Also note that you need to be sure to do something responsible with the created Buffer objects – I tried to help by only loading the Buffer if the callback function isn’t nil)

coincidentally, I just made a little interface to load buffers by drag and drop, which also stores them for named access, sharing in case it’s useful inspiration

Drag wav or aiff files onto the empty part of the window to load them into buffers, double click to name the buffers, drag files onto an existing buffer to overwrite it at the same index so any running synths will play the newly loaded buffer, option-click to free buffer and remove it from the list.

~bufs = ESBufList().makeWindow;

~bufs[\named_buf] // -> Buffer(1, ...
3 Likes