1 module test1.audioconfig;
2 
3 import pixelperfectengine.concrete.window;
4 import pixelperfectengine.audio.base.handler;
5 
6 import test1.app;
7 
8 import iota.audio.output;
9 import iota.audio.device;
10 import iota.audio.midi;
11 
12 import std.utf;
13 import std.conv : to;
14 import std.stdio;	//Bit ugly, but since we already have the command line window, let's use it for status messages!
15 
16 public class AudioConfig : Window {
17 	ListView listView_AudDevs;
18 	ListView listView_MidiDev;
19 	TextBox textBox_sampleRate;
20 	TextBox textBox_buffer;
21 	TextBox textBox_frame;
22 	Label label0;
23 	Label label1;
24 	Label label2;
25 	Button button_apply;
26 	AudioDevKit app;
27 	public this(AudioDevKit app) {
28 		super(Box(0, 0, 355, 250), "Audio and MIDI setup");
29 		this.app = app;
30 		listView_AudDevs = new ListView(new ListViewHeader(16, [30 ,300], ["Num" ,"Audio Device"]), null, "listView0", 
31 				Box(5, 20, 175, 200));
32 		listView_MidiDev = new ListView(new ListViewHeader(16, [30 ,300], ["Num" ,"MIDI Device"]), null, "listView1", 
33 				Box(180, 20, 350, 200));
34 		textBox_sampleRate = new TextBox("48000"d, "textBox0", Box(5, 225, 90, 245));
35 		textBox_buffer = new TextBox("512"d, "textBox1", Box(94, 225, 175, 245));
36 		textBox_frame = new TextBox("64"d, "textBox2", Box(180, 225, 257, 245));
37 		label0 = new Label("Sample rate:"d, "label0", Box(5, 205, 91, 225));
38 		label1 = new Label("Buffer size:"d, "label1", Box(95, 205, 175, 225));
39 		label2 = new Label("Frame size:"d, "label2", Box(180, 205, 257, 222));
40 		button_apply = new Button("Apply"d, "button0", Box(260, 225, 350, 245));
41 
42 		listView_AudDevs ~= new ListViewItem(16, ["-1", "Default"]);
43 
44 		string[] drivers = getOutputDeviceNames();
45 		foreach (size_t id, string key; drivers) {
46 			listView_AudDevs ~= new ListViewItem(16, [to!dstring(id), toUTF32(key)]);
47 		}
48 		drivers = getMIDIInputDevs();
49 		foreach (size_t id, string key; drivers) {
50 			listView_MidiDev ~= new ListViewItem(16, [to!dstring(id), toUTF32(key)]);
51 		}
52 
53 		button_apply.onMouseLClick = &button_apply_onClick;
54 
55 		textBox_sampleRate.setFilter(TextInputFieldType.IntegerP);
56 		textBox_buffer.setFilter(TextInputFieldType.IntegerP);
57 		textBox_frame.setFilter(TextInputFieldType.IntegerP);
58 
59 		addElement(listView_AudDevs);
60 		addElement(listView_MidiDev);
61 		addElement(textBox_sampleRate);
62 		addElement(textBox_buffer);
63 		addElement(textBox_frame);
64 		addElement(label0);
65 		addElement(label1);
66 		addElement(label2);
67 		addElement(button_apply);
68 	}
69 	protected void button_apply_onClick(Event ev) {
70 		import iota.audio.types;
71 		//create audio specs
72 		try {
73 			app.aS = AudioSpecs(predefinedFormats[PredefinedFormats.FP32], to!int(textBox_sampleRate.getText.text), 0, 2, 
74 					to!int(textBox_buffer.getText.text), Duration.init);
75 			int frameSize = to!int(textBox_frame.getText.text);
76 			app.adh = new AudioDeviceHandler(app.aS, frameSize, app.aS.bufferSize_slmp / frameSize);
77 			app.adh.initAudioDevice(to!int(listView_AudDevs.selectedElement[0].text.text));
78 			app.mm = new ModuleManager(app.adh);
79 			app.onStart();
80 			this.close();
81 
82 			if (listView_MidiDev.value != -1) {
83 				const int midiErrCode = openMIDIInput(app.midiIn, listView_MidiDev.value);
84 				if (app.midiIn !is null) {
85 					app.midiIn.midiInCallback = &app.midiInCallback;
86 					app.midiIn.start();
87 				}
88 			}
89 		} catch (Exception e) {
90 			handler.message("Audio initialization error!", toUTF32(e.msg));
91 		}
92 	}
93 }