Skip to content

Commit

Permalink
[plugins] Sample synthesizer showing preview icons
Browse files Browse the repository at this point in the history
  • Loading branch information
momentarylapse committed Sep 29, 2024
1 parent 426cf5a commit 882aefd
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 9 deletions.
10 changes: 10 additions & 0 deletions plugins/helper/tone.kaba
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,21 @@ func rel_pitch_name(pitch_rel: int) -> string
return "B"
return "???"

func subscript_int(i: i32) -> string
if i >= -1 and i <= 9
return ["₋₁", "₀", "₁", "₂", "₃", "₄", "₅", "₆", "₇", "₈", "₉"][i+1]
return str(i)

func pitch_name(pitch: int) -> string
var name_rel = rel_pitch_name(pitch_to_rel(pitch))
var name_oct = str(pitch_get_octave(pitch))
return name_rel + name_oct

func pitch_name_pretty(pitch: int) -> string
var name_rel = rel_pitch_name(pitch_to_rel(pitch))
var name_oct = subscript_int(pitch_get_octave(pitch))
return name_rel + name_oct


struct Chord
enum Type
Expand Down
6 changes: 4 additions & 2 deletions plugins/synthesizer/Sample.kaba
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,19 @@ class SamplePanel extends ConfigPanel
var override c: SampleSynthesizer&
func override __init__(_s: Module)
sc := &c.config
add_list_view("!expandx\\Note\\Sample", 0, 0, "list")
add_list_view("!expandx,format=ttti\\Index\\Note\\Sample\\Preview", 0, 0, "list")
event("list", on_select)

func override update()
reset("list")
for i in 0:MAX_PITCH
var name = ""
var preview = ""
if i < len(sc.samples)
for s in sc.samples[i]
name = s.origin.name
set_string("list", pitch_name(i) + "\\" + name)
preview = get_sample_preview(s.origin, c.session.view)
set_string("list", "{{i}}\\{{pitch_name_pretty(i)}}\\{{name}}\\{{preview}}")

func mut on_select()
var n = get_int("")
Expand Down
2 changes: 2 additions & 0 deletions plugins/tsunami/ui.kaba
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ var extern theme: ColorScheme

var extern clipboard: Clipboard&

func extern get_sample_preview(s: Sample, view: AudioView) -> string

func extern get_style_colors(p: hui.Panel, id: string) -> color{}


Expand Down
3 changes: 3 additions & 0 deletions src/plugins/PluginManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ namespace tsunami {

#define _offsetof(CLASS, ELEMENT) (int)( (char*)&((CLASS*)1)->ELEMENT - (char*)((CLASS*)1) )

string get_sample_preview(Sample *s, AudioView *view);

PluginManager::PluginManager() {
presets = new PresetManager;

Expand Down Expand Up @@ -215,6 +217,7 @@ void PluginManager::link_app_data() {
ext->link("draw_arrow_head", (void*)&draw_arrow_head);
ext->link("draw_arrow", (void*)&draw_arrow);
ext->link("interpolate_buffer", (void*)&BufferInterpolator::interpolate);
ext->link("get_sample_preview", (void*)&get_sample_preview);
ext->link("get_style_colors", (void*)&hui::get_style_colors);


Expand Down
9 changes: 5 additions & 4 deletions src/view/dialog/SampleSelectionDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace tsunami {

string render_sample(Sample *s, AudioView *view);
string get_sample_preview(Sample *s, AudioView *view);

SampleSelectionDialog::SampleSelectionDialog(Session *_session, hui::Panel *parent, Sample *old) :
hui::Dialog("sample_selection_dialog", parent->win)
Expand All @@ -41,8 +41,9 @@ SampleSelectionDialog::SampleSelectionDialog(Session *_session, hui::Panel *pare
}

SampleSelectionDialog::~SampleSelectionDialog() {
for (string &name: icon_names)
hui::delete_image(name);
// no, keep them forever! ...
//for (string &name: icon_names)
// hui::delete_image(name);
}

void SampleSelectionDialog::fill_list() {
Expand All @@ -51,7 +52,7 @@ void SampleSelectionDialog::fill_list() {
set_string(list_id, _("\\- none -\\"));
set_int(list_id, 0);
foreachi(Sample *s, weak(song->samples), i) {
icon_names.add(render_sample(s, session->view));
icon_names.add(get_sample_preview(s, session->view));
set_string(list_id, icon_names[i] + "\\" + song->get_time_str_long(s->buf->length) + "\\" + s->name);
if (s == selected)
set_int(list_id, i + 1);
Expand Down
8 changes: 5 additions & 3 deletions src/view/sidebar/SampleManagerConsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ void render_midi(Image &im, MidiNoteBuffer &m) {
}
}

string render_sample(Sample *s, AudioView *view) {
// TODO find a good strategy to delete obsolete images
// for now, assume we only have a limited number of samples/uids...
string get_sample_preview(Sample *s, AudioView *view) {
Image im;
im.create(SAMPLE_PREVIEW_WIDTH, SAMPLE_PREVIEW_HEIGHT, color(0, 0, 0, 0));
if (s->type == SignalType::Audio)
Expand All @@ -84,7 +86,7 @@ class SampleManagerItem : public obs::Node<VirtualBase> {
manager = _manager;
s = _s;
view = _view;
icon = render_sample(s, view);
icon = get_sample_preview(s, view);
s->out_death >> create_sink([this] { on_delete(); });
s->out_changed_by_action >> create_sink([this] { on_update(); });
s->out_reference >> create_sink([this] { on_update(); });
Expand All @@ -101,7 +103,7 @@ class SampleManagerItem : public obs::Node<VirtualBase> {
return;
int n = manager->get_index(s);
if (n >= 0) {
icon = render_sample(s, view);
icon = get_sample_preview(s, view);
manager->change_string(manager->id_list, n, str());
}
}
Expand Down

0 comments on commit 882aefd

Please sign in to comment.