Skip to content

Commit

Permalink
Various fixes for problems discovered while working on the Rochut Ana…
Browse files Browse the repository at this point in the history
…lysis program
  • Loading branch information
carlosemello committed Aug 5, 2017
1 parent e8827f9 commit 32778bf
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 23 deletions.
121 changes: 107 additions & 14 deletions MuMaterial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,11 @@ MuMaterial & MuMaterial::operator=(const MuMaterial & inMaterial) // [PUBLIC]
if(this == &inMaterial)
return *this;
// delete old storage...
delete [] voices;
voices = NULL;
if(voices)
{
delete [] voices;
voices = NULL;
}
int n = 0;
// check for input content...
if(inMaterial.voices != NULL)
Expand Down Expand Up @@ -255,8 +258,20 @@ MuMaterial & MuMaterial::operator*(short interval) // [PUBLIC]
MuMaterial::~MuMaterial(void)
{
lastError.Set(MuERROR_NONE);
int i;

// Leak Fix:
// in order to avoid memory leaks,
// before releasing the voice array, we need to go
// through each voice and release all the notes...
if(voices)
{
for(i = 0; i < numOfVoices; i++)
voices[i].Clear();
delete [] voices;
voices = NULL;
numOfVoices = 0;
}
}

// ======================================================================
Expand Down Expand Up @@ -298,6 +313,25 @@ float MuMaterial::Dur(void) // [PUBLIC]
return dur;
}

float MuMaterial::Dur(int voiceNumber)
{
float dur = 0;
lastError.Set(MuERROR_NONE);
if(voices != NULL)
{
if( ( voiceNumber >= 0 ) && ( voiceNumber < numOfVoices ) )
{
dur = voices[voiceNumber].Dur();
}
else
lastError.Set(MuERROR_INVALID_VOICE_NUMBER);
}
else
lastError.Set(MuERROR_MATERIAL_IS_EMPTY);

return dur;
}

// Voices
int MuMaterial::NumberOfVoices(void) // [PUBLIC]
{
Expand Down Expand Up @@ -430,6 +464,7 @@ void MuMaterial::SetVoice(int voiceNum, const MuMaterial & inMaterial, int inVoi
lastError.Set( err );
return;
}
voices[voiceNum].SetInstrumentNumber(tempVoice.InstrumentNumber());
}
else
{
Expand Down Expand Up @@ -837,6 +872,47 @@ long MuMaterial::NumberOfNotes(int voiceNumber)
return num;
}

float MuMaterial::MelodicDensity( int voiceNumber )
{
MuNote note;
MuError error;
long n,i;
float lastStart = -1;
float density = 0;
float dur = 0;
if(voices != NULL)
{
// if requested voice is valid...
if((voiceNumber < numOfVoices) && (voiceNumber >= 0))
{
dur = voices[voiceNumber].Dur();
if(dur > 0)
{
n = voices[voiceNumber].NumberOfNotes();
for(i = 0;i < n; i++)
{
error = voices[voiceNumber].GetNote(i, &note);
if(error.Get() == MuERROR_NONE)
{
if (note.Start() != lastStart)
{
density++;
}
lastStart = note.Start();
}
}
density /= dur;
}
}
else
lastError.Set(MuERROR_INVALID_VOICE_NUMBER);
}
else
lastError.Set(MuERROR_MATERIAL_IS_EMPTY);

return density;
}

void MuMaterial::SetNote(long noteNumber, MuNote inNote) // [PUBLIC]
{
lastError.Set(MuERROR_NONE);
Expand Down Expand Up @@ -2657,7 +2733,8 @@ void MuMaterial::LoadScore(string fileName, short mode) // [PUBLIC]
char tempLine[32];
int i, j;
MuNote theNote;

stringstream tables;

// get rid of any previous data in this material
Clear();

Expand All @@ -2675,7 +2752,7 @@ void MuMaterial::LoadScore(string fileName, short mode) // [PUBLIC]
{
// if function table found, store it..
case 'f':
AddFunctionTables(inputLine);
tables << inputLine << endl;
break;

// if note line found, ...
Expand Down Expand Up @@ -2719,6 +2796,10 @@ void MuMaterial::LoadScore(string fileName, short mode) // [PUBLIC]
break;
}
}
// if tables stream is not empty...
string temp = tables.str();
if (temp != "") // we save the tables to material...
SetFunctionTables(temp);
}
else
{
Expand Down Expand Up @@ -3040,12 +3121,19 @@ void MuMaterial::Sort( int voiceNumber, short field ) // [PUBLIC]
void MuMaterial::Clear(void)
{
MuError err(MuERROR_NONE);
if(voices != NULL)
{
delete [] voices;
voices = NULL;
numOfVoices = 0;
}
int i;
// Leak Fix:
// in order to avoid memory leaks,
// before releasing the voice array, we need to go
// through each voice and release all the notes...
if(voices)
{
for(i = 0; i < numOfVoices; i++)
voices[i].Clear();
delete [] voices;
voices = NULL;
numOfVoices = 0;
}
}

void MuMaterial::Show( void )
Expand Down Expand Up @@ -3077,10 +3165,15 @@ string MuMaterial::FunctionTables(void) // [PUBLIC]
void MuMaterial::SetDefaultFunctionTables(void) // [PUBLIC]
{
MuError err(MuERROR_NONE);
AddFunctionTables("f1 0 4096 10 1 .9 .1 .8 .2 .7 .3 .6 .4 .5");
AddFunctionTables("f2 0 4096 10 1 0 1 0 1 0 1 0 1");
AddFunctionTables("f3 0 4096 10 .1 .3 .5 .7 .5 .3 .1");
AddFunctionTables("f4 0 4096 10 .8 .6 .4 .2 .4 .6 .8");
stringstream tables;

tables << "f1 0 4096 10 1 .9 .1 .8 .2 .7 .3 .6 .4 .5" << endl;
tables << "f2 0 4096 10 1 0 1 0 1 0 1 0 1" << endl;
tables << "f3 0 4096 10 .1 .3 .5 .7 .5 .3 .1" << endl;
tables << "f4 0 4096 10 .8 .6 .4 .2 .4 .6 .8" << endl;

string temp = tables.str();
SetFunctionTables(temp);
}

void MuMaterial::SetFunctionTables(string inTables) // [PUBLIC]
Expand Down
57 changes: 53 additions & 4 deletions MuMaterial.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
code) in the derived work's source and documentation files. The original MuM
implementation was writen by Carlos Eduardo Mello, at the University of Brasilia.
* @version 1.0
* @version 1.0.4
* @author Carlos Eduardo Mello
* @date 3/5/2009
* @date 16/7/2016
**/

#ifndef _MU_MATERIAL_H_
Expand All @@ -53,9 +53,8 @@


// MUSIC CONSTANTS:

const short LOWEST_C = 24;
const short HIGHEST_C = 108;
const short HIGHEST_C = 108;
const short NUM_OF_SCALE_DEGREES = 7;
const short NUM_OF_OCTAVES = 8;
const short FULL_SCALE_SIZE = NUM_OF_SCALE_DEGREES * NUM_OF_OCTAVES;
Expand Down Expand Up @@ -402,6 +401,26 @@ class MuMaterial
*
**/
float Dur(void);

/**
* @brief Returns the duration of the requested voice inside material
*
* @details
* Returns the total duration of the requested voice. This value includes any offset
* between time zero and the first note's start time. In other words, Dur() value
* starts in time 0.0 and runs through the end of the last sounding note in the voice.
* If requested voice is not a valid voice number for the material, Dur() issues an error
* and returns 0.
*
* @param
* voiceNumber (int) - index of the requested voice
*
* @return
* float - time in seconds
*
**/
float Dur(int voiceNumber);


// VOICES

Expand Down Expand Up @@ -731,6 +750,36 @@ class MuMaterial
*
**/
long NumberOfNotes( int voiceNumber );


/**
* @brief Returns the melodic density of the requested voice
*
* @details
* MelodicDensity() returns a float indicating the average number of
* melodic notes per second in a given voice inside material. This
* indicator is measured here as the number of melodic notes
* within the requested voice divided by the voice's duration.
* Since we are only interested in the horizontal dimension, multiple
* notes articulated together are accounted as a single note.
*
* This kind of measurement may be used to compare melodic materials
* for the amount of movement within the line, as an aiding tool for
* choosing melodies of various characters.
*
* If material is empty, or voiceNumber is not a valid voice index number,
* or voiceNumber is not a valid voice, an error is issued and the method
* returns 0.
*
* @param
* voiceNumber (int) - voice index
*
* @return
* int - average number of notes per second
*
**/
float MelodicDensity( int voiceNumber );


/**
* @brief Sets note 'noteNumber' of voice '0' to 'inNote'
Expand Down
6 changes: 3 additions & 3 deletions MuNote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,15 @@ string MuNote::CsString(void)
cs_string += "\t";
sprintf(buff,"%.3f",Start());
cs_string += buff;
cs_string += "\t";
cs_string += "\t\t";
sprintf(buff,"%.3f",Dur());
cs_string += buff;
cs_string += "\t";
cs_string += "\t\t";
cs_string += PitchString();
cs_string += "\t\t";
sprintf(buff,"%.3f",Amp());
cs_string += buff;
cs_string += "\t";
cs_string += "\t\t";

pBlock = Params();
numberOfParams = pBlock.Num();
Expand Down
8 changes: 6 additions & 2 deletions MuParamBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ MuParamBlock & MuParamBlock::operator=(const MuParamBlock & inBlock)

if(this != &inBlock)
{
delete [] values;
// if there was any previously allocated
// memory, we get rid of it...
if(values)
delete [] values;

n = inBlock.numValues;
if(n > 0)
{
Expand All @@ -84,7 +88,7 @@ MuParamBlock & MuParamBlock::operator=(const MuParamBlock & inBlock)
// subscript
float& MuParamBlock::operator[](uShort i)
{
return values[i];
return values[i];
}

bool MuParamBlock::operator==(const MuParamBlock & inBlock)
Expand Down
1 change: 1 addition & 0 deletions MuUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,5 @@ extern void SortFloats( float * array, int size);
**/
extern void ShowInts( int * array, int size );


#endif

0 comments on commit 32778bf

Please sign in to comment.