Source code notes
The STS code allows MidiShare sequences to be written as standard CSound score files. It was conceived as the basis for a simple PRIE extension (a Win32 based Midi compositional environment by the same author). Altough not written with portability considerations in mind, it doesn't make use of platform or framework dependent routines so it should be compilable on others OS.
Because the look of a CSound score is mainly a question of habit and way of working, the code only offers basic formatting capabilities. For the same reason no comments are added to the final output.
The source is contained in the following files:
STS.cpp and h | STS code. |
STSUtils.cpp and h | Mainly a set of Hashtable-based routines (by Stephane Letz) to find and convert KeyOn KeyOff pairs to typeNote events. |
ThkFunc.h | Macros conversion for MidiShare Thunk release. |
All information needed to convert and write a sequence to a score file are contained in the following structure:
typedef struct TScore
{
unsigned char* scorefile; Output CSound
score file
unsigned char* addinfile; Additional file to
merge(GEN settings, others sections, etc.)
STSPitchConvert pc;.......Midi pitch conversion settings
PitchFunPtr PFunc;........Custom Midi pitch conversion routine
short pAction;............Identifies action in
PFunc
STSAmpliConvert ac;.......Midi velocity conversion settings
AmpliFunPtr AFunc;........Custom Midi velocity conversion routine
short aAction;............Identifies action in
AFunc
int maxamp;...............Amplitude value for
acAuto: (maxamp / 128) * vel
CustomFunPtr CFunc;.......Custom routine to handle MShare privates
events
short cAction;............Identifies action in
CFunc
STSAssignBy ab;...........Tracks/chans to CSound-instruments assignement
STSFieldOrder fo;.........p4 and p5 parameters order
float defdur;.............Default duration for
non-notes events
bool writesection;........Writes data as CSound
score or section
bool shiftref;............Shifts tracks numbers
by one (useful for format0 Midifiles)
float padout;.............Padding out value (CSound
f0 statement) for sections
float ptable[128];........Custom or default pitch-table
for pitch conversion
float atable[128];........Custom amplitude-table
for velocity conversion
bool events [149];........Events from source
sequence to consider
bool trkchn [256];........Tracks/Channels from
source sequence to consider
}TScore;
The above structure's fields are accessed after initialization by mean of the ScorePtr returned by the STS_OpenSession routine. This function requires as parameter the path and name of the score file to write. A session can be open this way:
ScorePtr sp;
sp = STS_OpenSession("c:\\test.sco");
The initialization routine allocates memory for the structure and assigns default values to the various fields.
The 3 functions pointers PFunc, AFunc and CFunc point to default (dummy) routines that can be changed as needed. They all require a ScorePtr as parameter:
typedef
float
(*PitchFunPtr) (ScorePtr sp, long pitch);
typedef
float
(*AmpliFunPtr) (ScorePtr sp, long ampli);
typedef
void
(*CustomFunPtr) (ScorePtr sp, char* str, long
e);
The corresponding pAction, aAction and cAction fields can be used to change the functions behavior (for instance in a switch) depending on their value.
The events array contains the MidiShare events codes that will be considered during the conversion process. Please note that not all the events that can be turned on (codes 0 - 148) are really supported. Support is included for:
Depending on the STSAssingBy value, the trkchn array contains the tracks (refnum) or channels that will be transformed to CSound instruments. If all events have a refnum value of 0, the shiftref boolean can be set to true so to force STS to convert refnum 0 to CSound instrument i1.
The ptable and atable arrays can be used in conjunction with the STS_LoadCustomTable function that allows importing of text files. By default the ptable array is initialized using an Hertz mapping table, while atable with values corrsponding to (maxamp / 128) * Midi velocity.
All default values can be changed with more suitable ones. When all is ok the main STS function: STS_WriteScore can be called. If the sequence to convert has events timestamped using dates in ticks, a conversion to milliseconds is necessary. Furthermore, KeyOn/Off events need to be translated to TypeNote. The file "StsUtils.cpp" contains routines that simplify the conversion process altough, obviously, you may prefer to use your owns.
Is given here a simple example that shows the usage of the STS code. Some of the default settings are changed before writing the score file:
ScorePtr sp;
/* scorefilepath
is the path of the score file to create, ScoreSeq
is a MidiSeqPtr to the sequence that will be converted, PTablefile
is the path of a custom pitch-table text file*/
if((sp = STS_OpenSession(scorefilepath))
!= NULL){ /*allocates and initialize*/
/*Instead that as Pch, Midi pitch will be converted using a previously written custom function*/
sp->pc = pcFunc;
sp->PFunc = MyCustomPitchRoutine;
/*Load a table for the custom Pitch function (and so specifying ttPitch), from a text table file. An offset of 60 is indicated so the first Midi note affected by the first file line will be C3. Notes in the range 0 - 59 will be converted using the values of the default Hertz table*/
STS_LoadCustomTable
(sp, PTablefile, ttPitch, 60, 127);
/*By default, amplitudes are calculated dividing the maxamp value by 128 and multiplying the result by the Midi velocity given at input. Here the value assigned to maxamp is changed*/
sp->maxamp = 7000;
/*write the score to file and frees memory*/
STS_WriteScore
(ScoreSeq, sp);
STS_CloseSession
(sp);
}
Alfio Fazio 1999