📄 melody.cpp
字号:
// Melody.cpp: implementation of the Melody class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "NokiaComposer.h"
#include "Melody.h"
#include "KeySequence.h"
#include "MelodyWindow.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
IMPLEMENT_SERIAL(Melody, CObject, SCHEMA_VERSION)
// +-------------------------------------------------------------
// |
// | Funci髇 : Melody::Melody
// | Descripci髇 :
// |
// +-------------------------------------------------------------
Melody::Melody()
{
Reset();
}
// +-------------------------------------------------------------
// |
// | Funci髇 : Melody::~Melody
// | Descripci髇 :
// |
// +-------------------------------------------------------------
Melody::~Melody()
{
}
// +-------------------------------------------------------------
// |
// | Funci髇 : Melody::SetTitle
// | Descripci髇 :
// |
// | title :
// |
// +-------------------------------------------------------------
void Melody::SetTitle( const char* title )
{
title_ = title;
}
// +-------------------------------------------------------------
// |
// | Funci髇 : Melody::GetTitle
// | Descripci髇 :
// |
// +-------------------------------------------------------------
const CString& Melody::GetTitle() const
{
return title_;
}
// +-------------------------------------------------------------
// |
// | Funci髇 : Melody::SetTempo
// | Descripci髇 :
// |
// | tempo :
// |
// +-------------------------------------------------------------
void Melody::SetTempo( int tempo )
{
tempo_ = tempo;
}
// +-------------------------------------------------------------
// |
// | Funci髇 : Melody::GetTempo
// | Descripci髇 :
// |
// +-------------------------------------------------------------
int Melody::GetTempo() const
{
return tempo_;
}
// +-------------------------------------------------------------
// |
// | Funci髇 : Melody::Serialize
// | Descripci髇 :
// |
// | &ar :
// |
// +-------------------------------------------------------------
void Melody::Serialize(CArchive &ar)
{
if( ar.IsStoring() )
{
ar << title_;
ar << tempo_;
ar << notes_.size() ;
NoteSequence::iterator it;
for(it = notes_.begin() ; it != notes_.end() ; ++it )
{
it->Serialize(ar);
}
}
else
{
int notesCount;
Note n;
ar >> title_;
ar >> tempo_;
ar >> notesCount;
while( notesCount-- )
{
n.Serialize(ar);
notes_.push_back(n);
}
}
}
// +-------------------------------------------------------------
// |
// | Funci髇 : Melody::Reset
// | Descripci髇 :
// |
// +-------------------------------------------------------------
void Melody::Reset()
{
title_ = "sin t韙ulo";
tempo_ = 110;
notes_.clear();
}
// +-------------------------------------------------------------
// |
// | Funci髇 : Melody::GetString
// | Descripci髇 :
// |
// +-------------------------------------------------------------
CString Melody::GetString() const
{
CString melodyString;
NoteSequence::const_iterator it = notes_.begin();
while( it != notes_.end() )
{
melodyString += it->GetString();
++it;
if( it != notes_.end() )
melodyString += " ";
}
return melodyString;
}
// +-------------------------------------------------------------
// |
// | Funci髇 : Melody::ParseString
// | Descripci髇 :
// |
// | melodyWindow :
// |
// +-------------------------------------------------------------
bool Melody::ParseString(MelodyWindow& melodyWindow)
{
Note note;
const char* p = melodyWindow.GetString();
const char* firstPos = p;
bool foundErrors = false;
melodyWindow.Lock();
melodyWindow.MarkAllNotesGood();
notes_.clear();
while( *p )
{
try
{
p = note.ParseString( p );
notes_.push_back( note );
}
catch(ParserException& pe)
{
int errorPos = p - firstPos;
int errorEnd = pe.GetNextNote() - firstPos;
melodyWindow.MarkBadNote(errorPos, errorEnd);
if( *pe.GetNextNote() == 0 )
TRACE("parece que es la ultima nota\n");
TRACE("%s: %s [%d..%d]\n", pe.GetDescription(), pe.GetNoteString(), errorPos, errorEnd);
p = pe.GetNextNote();
foundErrors = true;
}
}
melodyWindow.Unlock();
return ! foundErrors;
}
// +-------------------------------------------------------------
// |
// | Function : Melody::GetNotes
// | Description :
// |
// +-------------------------------------------------------------
const NoteSequence& Melody::GetNotes() const
{
return notes_;
}
// +-------------------------------------------------------------
// |
// | Funci髇 : Melody::GetNoteCount
// | Descripci髇 :
// |
// +-------------------------------------------------------------
int Melody::GetNoteCount() const
{
return notes_.size();
}
// +-------------------------------------------------------------
// |
// | Funci髇 : Melody::ParseKeySequence
// | Descripci髇 :
// |
// | keySequence :
// |
// +-------------------------------------------------------------
void Melody::ParseKeySequence(const KeySequence& keySequence)
{
notes_.clear();
KeySequenceIterator it = keySequence.GetKeySequence();
int octave = 1;
int duration = 4;
int silenceDuration = 4;
bool dot = false;
bool sharp = false;
Note::Tone tone;
// primera tecla
if( it.HasMoreElements() )
{
Key key = it.GetNext();
Assert(key.IsTone(), "la primera tecla deber韆 ser una nota");
tone = key.GetTone();
// el resto
while( it.HasMoreElements() )
{
Key key = it.GetNext();
if( key.IsTone() )
{
// hemos terminado con una nota, y empieza la siguiente (o un silencio)
if( tone == Note::Silence )
notes_.push_back( Note( tone, silenceDuration, octave, dot, sharp ) );
else
notes_.push_back( Note( tone, duration, octave, dot, sharp ) );
silenceDuration = duration;
tone = key.GetTone();
dot = key.HasDot();
sharp = false;
continue;
}
if( key == '#' )
{
sharp = true;
continue;
}
if( key == '8' )
{
if( tone == Note::Silence )
silenceDuration <<= 1;
else
duration <<= 1;
continue;
}
if( key == '9' )
{
if( tone == Note::Silence )
silenceDuration >>= 1;
else
duration >>= 1;
continue;
}
if( key == '*' )
{
++octave;
if( octave > 3 )
octave = 1;
continue;
}
}
notes_.push_back( Note( tone, duration, octave, dot, sharp ) );
}
}
// +-------------------------------------------------------------
// |
// | Funci髇 : Melody::OctaveDown
// | Descripci髇 :
// |
// +-------------------------------------------------------------
void Melody::OctaveDown()
{
NoteSequence::iterator it = notes_.begin();
while( it != notes_.end() )
{
it->OctaveDown();
++it;
}
}
// +-------------------------------------------------------------
// |
// | Funci髇 : Melody::OctaveUp
// | Descripci髇 :
// |
// +-------------------------------------------------------------
void Melody::OctaveUp()
{
NoteSequence::iterator it = notes_.begin();
while( it != notes_.end() )
{
it->OctaveUp();
++it;
}
}
// +-------------------------------------------------------------
// |
// | Funci髇 : Melody::SemitoneDown
// | Descripci髇 :
// |
// +-------------------------------------------------------------
void Melody::SemitoneDown()
{
NoteSequence::iterator it = notes_.begin();
while( it != notes_.end() )
{
it->SemitoneDown();
++it;
}
}
// +-------------------------------------------------------------
// |
// | Funci髇 : Melody::SemitoneUp
// | Descripci髇 :
// |
// +-------------------------------------------------------------
void Melody::SemitoneUp()
{
NoteSequence::iterator it = notes_.begin();
while( it != notes_.end() )
{
it->SemitoneUp();
++it;
}
}
// +-------------------------------------------------------------
// |
// | Funci髇 : Melody::CanOctaveUp
// | Descripci髇 :
// |
// +-------------------------------------------------------------
bool Melody::CanOctaveUp() const
{
NoteSequence::const_iterator it = notes_.begin();
bool result = true;
while( result && it != notes_.end() )
{
result &= it->CanOctaveUp();
++it;
}
return result;
}
// +-------------------------------------------------------------
// |
// | Funci髇 : Melody::CanOctaveDown
// | Descripci髇 :
// |
// +-------------------------------------------------------------
bool Melody::CanOctaveDown() const
{
NoteSequence::const_iterator it = notes_.begin();
bool result = true;
while( result && it != notes_.end() )
{
result &= it->CanOctaveDown();
++it;
}
return result;
}
// +-------------------------------------------------------------
// |
// | Funci髇 : Melody::CanSemitoneUp
// | Descripci髇 :
// |
// +-------------------------------------------------------------
bool Melody::CanSemitoneUp() const
{
NoteSequence::const_iterator it = notes_.begin();
bool result = true;
while( result && it != notes_.end() )
{
result &= it->CanSemitoneUp();
++it;
}
return result;
}
// +-------------------------------------------------------------
// |
// | Funci髇 : Melody::CanSemitoneDown
// | Descripci髇 :
// |
// +-------------------------------------------------------------
bool Melody::CanSemitoneDown() const
{
NoteSequence::const_iterator it = notes_.begin();
bool result = true;
while( result && it != notes_.end() )
{
result &= it->CanSemitoneDown();
++it;
}
return result;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -