Posted: 30 Apr 2014, 04:19
Hi again,
Trying to learn how multi-dimensional arrays work. The following script seems to work fine sometimes but causes "access violation" errors at other times. Sometimes crashes HH. Can anyone tell me if the script is "wrong" and in what way(s)? Thanks!
//////////////////////////
// 2 Dimensional MIDI array sandbox
/////////////////////////
// parameters declaration
var MIDI_in : TParameter;
var chordCount : TParameter; // count of chords in memory
var cCount : integer; // total number of recorded chords
// initialisation : create parameters
procedure init;
begin
MIDI_in := CreateParam('MIDI_in',ptMidi);
chordCount := CreateParam('chordCount',ptDataField);
SetIsOutPut(MIDI_in,false);
SetIsInput(chordCount,false);
SetFormat(chordCount,'%.1f');
cCount := 0;
end;
// Global variables
var i : integer;
var nCount : integer; // count of notes in current chord
var nbOfMidi : integer; // length of received MIDI array
var Mdat : TMidi; // single message of received MIDI data
//var tnMdat : array of TMidi; // temporary array of MIDI notes
type tnMdat = array of TMidi; // temporary array of MIDI notes
var chordsMdat : array of tnMdat; // final array of MIDI chords
Procedure Callback(N:integer);
begin
case n of
0: begin // activity on MIDI input
nbOfMidi := GetLength(MIDI_in); // get the number of incoming midi codes
if nbOfMidi > 0 then
begin
for i := 0 to nbOfMidi-1 do // loop for all input codes, for polyphonic data (chords)
begin
GetMidiArrayValue(MIDI_in,i,Mdat); // get each code
if Mdat.msg = 144 then // if note on
begin
nCount := nCount+1; // inc note on count
setValue(chordCount,nCount);
SetArrayLength(chordsMdat,cCount); // set to chord count
chordsMdat[cCount,i] := Mdat;
itrace(chordsMdat[cCount,i].data1);
itrace(cCount);
setValue(chordCount,nCount);
end;
if Mdat.msg = 128 then // if note off
begin
nCount := nCount-1; // dec note count
if nCount = 0 then // if all notes are off
begin
cCount := cCount+1; // inc chord count
end;
end;
end;
end;
end;
end;
end;
Trying to learn how multi-dimensional arrays work. The following script seems to work fine sometimes but causes "access violation" errors at other times. Sometimes crashes HH. Can anyone tell me if the script is "wrong" and in what way(s)? Thanks!
//////////////////////////
// 2 Dimensional MIDI array sandbox
/////////////////////////
// parameters declaration
var MIDI_in : TParameter;
var chordCount : TParameter; // count of chords in memory
var cCount : integer; // total number of recorded chords
// initialisation : create parameters
procedure init;
begin
MIDI_in := CreateParam('MIDI_in',ptMidi);
chordCount := CreateParam('chordCount',ptDataField);
SetIsOutPut(MIDI_in,false);
SetIsInput(chordCount,false);
SetFormat(chordCount,'%.1f');
cCount := 0;
end;
// Global variables
var i : integer;
var nCount : integer; // count of notes in current chord
var nbOfMidi : integer; // length of received MIDI array
var Mdat : TMidi; // single message of received MIDI data
//var tnMdat : array of TMidi; // temporary array of MIDI notes
type tnMdat = array of TMidi; // temporary array of MIDI notes
var chordsMdat : array of tnMdat; // final array of MIDI chords
Procedure Callback(N:integer);
begin
case n of
0: begin // activity on MIDI input
nbOfMidi := GetLength(MIDI_in); // get the number of incoming midi codes
if nbOfMidi > 0 then
begin
for i := 0 to nbOfMidi-1 do // loop for all input codes, for polyphonic data (chords)
begin
GetMidiArrayValue(MIDI_in,i,Mdat); // get each code
if Mdat.msg = 144 then // if note on
begin
nCount := nCount+1; // inc note on count
setValue(chordCount,nCount);
SetArrayLength(chordsMdat,cCount); // set to chord count
chordsMdat[cCount,i] := Mdat;
itrace(chordsMdat[cCount,i].data1);
itrace(cCount);
setValue(chordCount,nCount);
end;
if Mdat.msg = 128 then // if note off
begin
nCount := nCount-1; // dec note count
if nCount = 0 then // if all notes are off
begin
cCount := cCount+1; // inc chord count
end;
end;
end;
end;
end;
end;
end;