public int Load(string FilePath) { Directory.SetCurrentDirectory(Path.GetDirectoryName(FilePath)); int LastOpcode = 0;//0=Null,1=Group,2=Region SFZGroup Group = null; SFZRegion Region = null; string[] Lines = File.ReadAllLines(FilePath); foreach (string line in Lines) { string[] Opcodes = line.Split(new string[] { " ", "\t" }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < Opcodes.Length; i++) { if (Opcodes[i].Trim().StartsWith("//")) break; else if (Opcodes[i].Trim() == "<group>") { if (Region != null) { if (Region.GenerateStream() == 0) { if (Group != null) Group.Regions.Add(Region); else Regions.Add(Region); } Region = null; } if (Group != null) Groups.Add(Group); Group = new SFZGroup(); LastOpcode = 1; } else if (Opcodes[i].Trim() == "<region>") { if (Region != null) { if (Region.GenerateStream() == 0) { if (Group != null) Group.Regions.Add(Region); else Regions.Add(Region); } } Region = new SFZRegion(Group); LastOpcode = 2; } else { string tmp = Opcodes[i]; while (Opcodes.Length > (i + 1) && !Opcodes[i + 1].Contains("=")) tmp += (" " + Opcodes[++i]); Opcodes[i] = tmp; if (Opcodes[i].Trim().StartsWith("sample=")) { if (LastOpcode == 1) Group.SamplePath = GetOpcodeValue(Opcodes[i]); else if (LastOpcode == 2) Region.SamplePath = GetOpcodeValue(Opcodes[i]); } else if (Opcodes[i].Trim().StartsWith("key=")) { byte key = 0; if (!Byte.TryParse(GetOpcodeValue(Opcodes[i]), out key)) key = (byte)Note.getNoteFromString(GetOpcodeValue(Opcodes[i])); /*try { key = Convert.ToByte(GetOpcodeValue(Opcodes[i])); } catch (Exception) { key = (byte)Note.getNoteFromString(GetOpcodeValue(Opcodes[i])); }*/ if (LastOpcode == 1) { Group.lokey = Group.hikey = key; } else if (LastOpcode == 2) { Region.lokey = Region.hikey = key; } } else if (Opcodes[i].Trim().StartsWith("lokey=")) { byte lokey = 0; if (!Byte.TryParse(GetOpcodeValue(Opcodes[i]), out lokey)) lokey = (byte)Note.getNoteFromString(GetOpcodeValue(Opcodes[i])); /*try { lokey = Convert.ToByte(GetOpcodeValue(Opcodes[i])); } catch (Exception) { lokey = (byte)Note.getNoteFromString(GetOpcodeValue(Opcodes[i])); }*/ if (LastOpcode == 1) { Group.lokey = lokey; } else if (LastOpcode == 2) { Region.lokey = lokey; } } else if (Opcodes[i].Trim().StartsWith("hikey=")) { byte hikey = 0; if (!Byte.TryParse(GetOpcodeValue(Opcodes[i]), out hikey)) hikey = (byte)Note.getNoteFromString(GetOpcodeValue(Opcodes[i])); /*try { hikey = Byte.TryParse(Convert.ToByte(GetOpcodeValue(Opcodes[i])); } catch (Exception) { hikey = (byte)Note.getNoteFromString(GetOpcodeValue(Opcodes[i])); }*/ if (LastOpcode == 1) { Group.hikey = hikey; } else if (LastOpcode == 2) { Region.hikey = hikey; } } else if (Opcodes[i].Trim().StartsWith("lovel=")) { byte lovel = Convert.ToByte(GetOpcodeValue(Opcodes[i])); if (LastOpcode == 1) { Group.lovel = lovel; } else if (LastOpcode == 2) { Region.lovel = lovel; } } else if (Opcodes[i].Trim().StartsWith("hivel=")) { byte hivel = Convert.ToByte(GetOpcodeValue(Opcodes[i])); if (LastOpcode == 1) { Group.hivel = hivel; } else if (LastOpcode == 2) { Region.hivel = hivel; } } else if (Opcodes[i].Trim().StartsWith("lorand=")) { float lorand = (float)Convert.ToDouble(GetOpcodeValue(Opcodes[i]), System.Globalization.CultureInfo.InvariantCulture); if (LastOpcode == 1) { Group.lorand = lorand; } else if (LastOpcode == 2) { Region.lorand = lorand; } } else if (Opcodes[i].Trim().StartsWith("hirand=")) { float hirand = (float)Convert.ToDouble(GetOpcodeValue(Opcodes[i]), System.Globalization.CultureInfo.InvariantCulture); if (LastOpcode == 1) { Group.hirand = hirand; } else if (LastOpcode == 2) { Region.hirand = hirand; } } else if (Opcodes[i].Trim().StartsWith("seq_length=")) { byte seq_length = Convert.ToByte(GetOpcodeValue(Opcodes[i])); if (LastOpcode == 1) { Group.seq_length = seq_length; } else if (LastOpcode == 2) { Region.seq_length = seq_length; } } else if (Opcodes[i].Trim().StartsWith("seq_position=")) { byte seq_position = Convert.ToByte(GetOpcodeValue(Opcodes[i])); if (LastOpcode == 1) { Group.seq_position = seq_position; } else if (LastOpcode == 2) { Region.seq_position = seq_position; } } else if (Opcodes[i].Trim().StartsWith("locc")) { byte locc = Convert.ToByte(GetOpcodeValue(Opcodes[i])); if (LastOpcode == 1) { Group.locc[Convert.ToByte(GetOpcodeCC(Opcodes[i]))] = locc; } else if (LastOpcode == 2) { Region.locc[Convert.ToByte(GetOpcodeCC(Opcodes[i]))] = locc; } } else if (Opcodes[i].Trim().StartsWith("hicc")) { byte hicc = Convert.ToByte(GetOpcodeValue(Opcodes[i])); if (LastOpcode == 1) { Group.hicc[Convert.ToByte(GetOpcodeCC(Opcodes[i]))] = hicc; } else if (LastOpcode == 2) { Region.hicc[Convert.ToByte(GetOpcodeCC(Opcodes[i]))] = hicc; } } //else Console.WriteLine(Opcodes[i].Trim()); } } } if (Region != null) { if (Region.GenerateStream() == 0) { if (Group != null) { Group.Regions.Add(Region); Groups.Add(Group); } else Regions.Add(Region); } } return (0); }
public void MIDI_NoteOn(byte Note, byte Velocity) { float Rand = (float)random.NextDouble(); if (LearnRegion != null) { LearnRegion.lokey = LearnRegion.hikey = Note; LearnRegion = null; if (SFZUpdated != null) SFZUpdated(); } foreach (SFZRegion r in Regions) if (r.cc && Note >= r.lokey && Note <= r.hikey && Velocity >= r.lovel && Velocity <= r.hivel && Rand >= r.lorand && Rand <= r.hirand) { if (r.seq_length + 1 == r.Group.seq_counter) { r.Group.seq_counter = 1; } if (r.Group.seq_counter == r.seq_position) { r.Play(Velocity); r.Group.seq_counter++; } } foreach (SFZGroup g in Groups) { if (g.cc && Note >= g.lokey && Note <= g.hikey && Velocity >= g.lovel && Velocity <= g.hivel && Rand >= g.lorand && Rand <= g.hirand) { foreach (SFZRegion r in g.Regions) if (r.cc && Note >= r.lokey && Note <= r.hikey && Velocity >= r.lovel && Velocity <= r.hivel && Rand >= r.lorand && Rand <= r.hirand) { if (r.seq_length + 1 == r.Group.seq_counter) { r.Group.seq_counter = 1; } if (r.Group.seq_counter == r.seq_position) { r.Play(Velocity); r.Group.seq_counter++; } } } } }
public void Learn(SFZRegion L) { LearnRegion = L; }