- Main Page
- Namespaces
- Classes
- Files
- Directories
- Related Pages

- Alphabetical List
- Class List
- Class Hierarchy
- Class Members

# Exponent::Midi::CMidiFile Class Reference

List of all members.

## Detailed Description

Provides SMF i/o.

Midi file io, currently only supports types 0 and 1 (who the hell uses type 2 midi files :S??)
All read operations are carried out as follows:

     // First we check that its a valid midi file
     if (!CMidiFile::isValidFormat("Path/To/Midi/File.mid"))
     {
            cout << "This is not a midi file" << endl;
            return false;
     }

     // Obviously a valid file, we can create the midi file
     CMidiFile file;

     // Open
     if (!file.openFile(CFileStream::e_input, "Path/To/Midi/File.mid"))
     {
            cout << "Failed to open the midi file" << endl;
            return false;
     }

     // Midi files are read in to midi sequences
     CMidiSequence sequence(sampleRate);

     // Now read the file in to the sequence
     if (!file.readFile(sequence))
     {
            cout << "Failed to read the midi file sequence" << endl;
            return false;
     }

     // The sequence is valid if we go to here, now you can use it for anything you want....

All write operations are carried out as follows:

     // Setup the sequence
     CMidiSequence sequence;

     // Add your events
     ...

     // Now write
     CMidiFile file;

     // Open
     if (!file.openFile(CFileStream::e_output, "Path/To/Midi/File.mid"))
     {
            cout << "Failed to open file to write" << endl;
            return false;
     }

     // Now we can write the file
     if (!file.writeFile(sequence))
     {
            cout << "Failed to write file" << endl;
            return false;
     }

See also:
  CMidiSequence

  CMidiTrack

Date:
  23/08/2004

Author:
  Paul Chana

Version:
  1.0.0 Initial version

Note:
  All contents of this source code are copyright 2005 Exp Digital Uk.
  This source file is covered by the licence conditions of the Infinity API. You should have recieved a copy
  with the source code. If you didnt, please refer to http://www.expdigital.co.uk All content is the Intellectual property of Exp Digital Uk.
  Certain sections of this code may come from other sources. They are credited where applicable.
  If you have comments, suggestions or bug reports please visit http://support.expdigital.co.uk

  Documented online at http://www.borg.com/~jglatt/tech/midifile.htm

Id
  CMidiFile.hpp,v 1.3 2007/02/08 21:08:09 paul Exp

Definition at line 107 of file CMidiFile.hpp.

## Public Member Functions

- bool closeFile ()
- CMidiFile ()
- bool openFile (CFileStream::EStreamMode mode, const CSystemString &filename)
- bool readFile (CMidiSequence &sequence)
- bool writeFile (const CMidiSequence &sequence)
- virtual ~CMidiFile ()

## Static Public Member Functions

- static CString getFileExtension ()
- static bool isValidFormat (const CSystemString &filename)

## Static Protected Member Functions

- static void computeSamplesPerTick (SMTHDChunk &headChunk, const double sampleRate, const double bpm)
- static double computeTicksPerSample (const double sampleRate, const double bpm)
- static bool isValidMTHDChunk (const SMTHDChunk &chunk)
- static bool isValidMTRKChunk (const SMTRKChunk &chunk)
- static bool isWritableEvent (const CMidiEvent &event)
- static void processMidiSequence (SMTHDChunk &headChunk, CMidiSequence &sequence)
- static bool readFileHeader (CFileStream &stream, SMTHDChunk &chunk)
- static bool readTrackChunks (CFileStream &stream, SMTHDChunk &headChunk, CMidiSequence &sequence)
- static unsigned long readVariableLengthUnsignedLong (CFileStream &stream, int &sizeRead)
- static bool writeFileHeader (CFileStream &stream, const CMidiSequence &sequence)
- static void writeMetaTrack (CFileStream &stream, const CMidiSequence &sequence)
- static void writeTempo (CFileStream &stream, const double bpm)
- static void writeTimeSignature (CFileStream &stream, const CTimeSignature ts)
- static bool writeTrack (CFileStream &stream, const CMidiTrack &track, const double ticksPerSample)
- static void writeTrackEnd (CFileStream &stream)
- static void writeTrackMidiChannel (CFileStream &stream, const long channel)
- static void writeTrackName (CFileStream &stream, const CString &name)
- static void writeVariableLengthUnsignedLong (CFileStream &stream, const unsigned long value)

## Protected Attributes

- CFileStream m_stream

## Static Protected Attributes

- static const char CMIDI_FILE_EVENT_SIZES []
- static const unsigned short CMIDI_FILE_PPQ = 960

## Classes

- struct SMidiEvent
    MidiEvent header. More...
- struct SMTHDChunk
    Chunk header for midi files. More...
- struct SMTRKChunk
    Track header for midi files. More...

## Constructor & Destructor Documentation

Exponent::Midi::CMidiFile::CMidiFile | ( |  | ) |

Construction

virtual Exponent::Midi::CMidiFile::~CMidiFile | ( |  | ) | `[virtual]`

Destruction

## Member Function Documentation

bool Exponent::Midi::CMidiFile::closeFile | ( |  | ) |

Close the file

Return values:

  | bool | True if closed properly, false otherwise

static void Exponent::Midi::CMidiFile::computeSamplesPerTick | ( | SMTHDChunk & | headChunk,
|  | const double | sampleRate,
|  | const double | bpm |
| ) |  |  | `[static, protected]`

Compute teh samples per tick

Parameters:

  | headChunk | The head chunk to fill in
  | sampleRate | The sample rate of the sequence
  | bpm | The bpm of the track, ignored if SMPTE timing being used

static double Exponent::Midi::CMidiFile::computeTicksPerSample | ( | const double | sampleRate,
|  | const double | bpm |
| ) |  |  | `[static, protected]`

Compute the number of ticks per sample

Parameters:

  | sampleRate | The samplerate of the sequence
  | bpm | The bpm of the sequence

Return values:

  | double | The number of ticks every sample

static CString Exponent::Midi::CMidiFile::getFileExtension | ( |  | ) | `[static]`

Get the extension of the file format, each array entry sould contain one string

Return values:

  | CString | The extension of a midi file

static bool Exponent::Midi::CMidiFile::isValidFormat | ( | const CSystemString & | filename | ) | `[static]`

Check if a file is a valid format of this file format

Parameters:

  | filename | The name of the file to load

Return values:

  | bool | True if the file format matches (checks actual binary data, not just extension

static bool Exponent::Midi::CMidiFile::isValidMTHDChunk | ( | const SMTHDChunk & | chunk | ) | `[static, protected]`

Is actually an MTHD chunk

Parameters:

  | chunk | The chunk to check

Return values:

  | bool | True if MTHD chunk

static bool Exponent::Midi::CMidiFile::isValidMTRKChunk | ( | const SMTRKChunk & | chunk | ) | `[static, protected]`

Is actually an MTRK chunk

Parameters:

  | chunk | The chunk to check

Return values:

  | bool | True if MTRK chunk

static bool Exponent::Midi::CMidiFile::isWritableEvent | ( | const CMidiEvent & | event | ) | `[static, protected]`

Is a writable event

Parameters:

  | event | The event to consider

Return values:

  | bool | True if writable event, false otherwise

bool Exponent::Midi::CMidiFile::openFile | ( | CFileStream::EStreamMode | mode,
|  | const CSystemString & | filename |
| ) |  |  |

Open the file

Parameters:

  | mode | The mode of opening, read or write
  | filename | The name of the file to open

Return values:

  | bool | True if stream is opened

static void Exponent::Midi::CMidiFile::processMidiSequence | ( | SMTHDChunk & | headChunk,
|  | CMidiSequence & | sequence |
| ) |  |  | `[static, protected]`

Post process a midi track

Parameters:

  | headChunk | The head chunk to get information from
  | sequence | The sequence to process

bool Exponent::Midi::CMidiFile::readFile | ( | CMidiSequence & | sequence | ) |

Read the file

Parameters:

  | sequence | The sequence to fill in

Return values:

  | bool | True if sequence is filled properly, false on error

static bool Exponent::Midi::CMidiFile::readFileHeader | ( | CFileStream & | stream,
|  | SMTHDChunk & | chunk |
| ) |  |  | `[static, protected]`

Is this a valid midi file

Parameters:

  | stream | The stream to read
  | chunk | The chunk to fill in

Return values:

  | bool | True if midi file format

static bool Exponent::Midi::CMidiFile::readTrackChunks | ( | CFileStream & | stream,
|  | SMTHDChunk & | headChunk,
|  | CMidiSequence & | sequence |
| ) |  |  | `[static, protected]`

Read the tracks

Parameters:

  | stream | The stream to read
  | headChunk | The head chunk to fill in
  | sequence | The sequence to fill in

Return values:

  | bool | True if read correctly, false otherwise

static unsigned long Exponent::Midi::CMidiFile::readVariableLengthUnsignedLong | ( | CFileStream & | stream,
|  | int & | sizeRead |
| ) |  |  | `[static, protected]`

Write a variable length unsigned long

Parameters:

  | sizeRead | On return holds the size of the data read in
  | stream | The stream to write to

Return values:

  | unsigned | long The variable length unsigned long as read from disk

bool Exponent::Midi::CMidiFile::writeFile | ( | const CMidiSequence & | sequence | ) |

WRite the file

Parameters:

  | sequence | The sequence to write, expected to be in sorted order

Return values:

  | bool | True if sequence is written properly, false otherwise

static bool Exponent::Midi::CMidiFile::writeFileHeader | ( | CFileStream & | stream,
|  | const CMidiSequence & | sequence |
| ) |  |  | `[static, protected]`

Write the header

Parameters:

  | stream | The stream to write
  | sequence | The sequence to write from

Return values:

  | bool | True if wrote header properly

static void Exponent::Midi::CMidiFile::writeMetaTrack | ( | CFileStream & | stream,
|  | const CMidiSequence & | sequence |
| ) |  |  | `[static, protected]`

Write the meta track

Parameters:

  | stream | The stream to write
  | sequence | The sequence to write from

static void Exponent::Midi::CMidiFile::writeTempo | ( | CFileStream & | stream,
|  | const double | bpm |
| ) |  |  | `[static, protected]`

Write the tempo in as a meta event

Parameters:

  | stream | The stream to read
  | bpm | The bpm

static void Exponent::Midi::CMidiFile::writeTimeSignature | ( | CFileStream & | stream,
|  | const CTimeSignature | ts |
| ) |  |  | `[static, protected]`

Write the tempo in as a meta event

Parameters:

  | stream | The stream to read
  | ts | The time signature

static bool Exponent::Midi::CMidiFile::writeTrack | ( | CFileStream & | stream,
|  | const CMidiTrack & | track,
|  | const double | ticksPerSample |
| ) |  |  | `[static, protected]`

Write the tracks

Parameters:

  | stream | The stream to read
  | track | The track to write
  | ticksPerSample | The number of ticks per sample

See also:
  computeTicksPerSample

Return values:

  | bool | True if written correctly, false otherwise

static void Exponent::Midi::CMidiFile::writeTrackEnd | ( | CFileStream & | stream | ) | `[static, protected]`

Write the end of a track

Parameters:

  | stream | The stream to read

static void Exponent::Midi::CMidiFile::writeTrackMidiChannel | ( | CFileStream & | stream,
|  | const long | channel |
| ) |  |  | `[static, protected]`

Write the midi channel of a track

Parameters:

  | channel | The midi channel
  | stream | The stream to read

static void Exponent::Midi::CMidiFile::writeTrackName | ( | CFileStream & | stream,
|  | const CString & | name |
| ) |  |  | `[static, protected]`

Write the name of a track

Parameters:

  | name | THe name of the track
  | stream | The stream to read

static void Exponent::Midi::CMidiFile::writeVariableLengthUnsignedLong | ( | CFileStream & | stream,
|  | const unsigned long | value |
| ) |  |  | `[static, protected]`

Write a variable length unsigned long

Parameters:

  | value | The value to write
  | stream | The stream to write to

## Member Data Documentation

const char Exponent::Midi::CMidiFile::CMIDI_FILE_EVENT_SIZES[]`[static, protected]`

Size of all the default events. Dont even try to understand this, it works ok ;)

Definition at line 368 of file CMidiFile.hpp.

const unsigned short Exponent::Midi::CMidiFile::CMIDI_FILE_PPQ = 960`[static, protected]`

PPQ size for output files

Definition at line 369 of file CMidiFile.hpp.

CFileStream Exponent::Midi::CMidiFile::m_stream`[protected]`

The file stream that we have

Definition at line 373 of file CMidiFile.hpp.