Logo Search packages:      
Sourcecode: scummvm version File versions  Download package

sound_2gs.h

/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * $URL: https://scummvm.svn.sourceforge.net/svnroot/scummvm/scummvm/tags/release-1-2-1/engines/agi/sound_2gs.h $
 * $Id: sound_2gs.h 49757 2010-06-15 10:36:54Z sev $
 *
 */

#ifndef AGI_SOUND_2GS_H
#define AGI_SOUND_2GS_H

#include "common/frac.h"
#include "sound/audiostream.h"

namespace Agi {

#define BUFFER_SIZE     410

// Apple IIGS MIDI uses 60 ticks per second (Based on tests with Apple IIGS
// KQ1 and SQ1 under MESS 0.124a). So we make the audio buffer size to be a
// 1/60th of a second in length. That should be getSampleRate() / 60 samples
// in length but as getSampleRate() is always 22050 at the moment we just use
// the hardcoded value of 368 (22050/60 = 367.5 which rounds up to 368).
// FIXME: Use getSampleRate() / 60 rather than a hardcoded value
#define IIGS_BUFFER_SIZE 368

// MIDI command values (Shifted right by 4 so they're in the lower nibble)
#define MIDI_CMD_NOTE_OFF        0x08
#define MIDI_CMD_NOTE_ON         0x09
#define MIDI_CMD_CONTROLLER      0x0B
#define MIDI_CMD_PROGRAM_CHANGE  0x0C
#define MIDI_CMD_PITCH_WHEEL     0x0E
// Whole MIDI byte values (Command and channel info together)
#define MIDI_BYTE_STOP_SEQUENCE  0xFC
#define MIDI_BYTE_TIMER_SYNC     0xF8

00054 struct IIgsEnvelopeSegment {
      uint8 bp;
00056       uint16 inc; ///< 8b.8b fixed point, very probably little endian
};

#define ENVELOPE_SEGMENT_COUNT 8
00060 struct IIgsEnvelope {
      IIgsEnvelopeSegment seg[ENVELOPE_SEGMENT_COUNT];

      /** Reads an Apple IIGS envelope from then given stream. */
      bool read(Common::SeekableReadStream &stream);
};

// 2**(1/12) i.e. the 12th root of 2
#define SEMITONE 1.059463094359295

// C6's frequency is A4's (440 Hz) frequency but one full octave and three semitones higher
// i.e. C6_FREQ = 440 * pow(2.0, 15/12.0)
#define C6_FREQ 1046.502261202395

// Size of the SIERRASTANDARD file (i.e. the wave file i.e. the sample data used by the instruments).
#define SIERRASTANDARD_SIZE 65536

// Maximum number of instruments in an Apple IIGS instrument set.
// Chosen empirically based on Apple IIGS AGI game data, increase if needed.
#define MAX_INSTRUMENTS 28

00081 struct IIgsWaveInfo {
      uint8 top;
      uint addr;
      uint size;
// Oscillator channel
#define OSC_CHANNEL_RIGHT 0
#define OSC_CHANNEL_LEFT  1
      uint channel;
// Oscillator mode
#define OSC_MODE_LOOP     0
#define OSC_MODE_ONESHOT  1
#define OSC_MODE_SYNC_AM  2
#define OSC_MODE_SWAP     3
      uint mode;
      bool halt;
00096       int16 relPitch; ///< Relative pitch in semitones (Signed 8b.8b fixed point)

      /** Reads an Apple IIGS wave information structure from the given stream. */
      bool read(Common::SeekableReadStream &stream, bool ignoreAddr = false);
      bool finalize(Common::SeekableReadStream &uint8Wave);
};

// Number of waves per Apple IIGS sound oscillator
#define WAVES_PER_OSCILLATOR 2

/** An Apple IIGS sound oscillator. Consists always of two waves. */
00107 struct IIgsOscillator {
      IIgsWaveInfo waves[WAVES_PER_OSCILLATOR];

      bool finalize(Common::SeekableReadStream &uint8Wave);
};

// Maximum number of oscillators in an Apple IIGS instrument.
// Chosen empirically based on Apple IIGS AGI game data, increase if needed.
#define MAX_OSCILLATORS 4

/** An Apple IIGS sound oscillator list. */
00118 struct IIgsOscillatorList {
00119       uint count; ///< Oscillator count
00120       IIgsOscillator osc[MAX_OSCILLATORS]; ///< The oscillators

      /** Indexing operators for easier access to the oscillators. */
00123       const IIgsOscillator &operator()(uint index) const { return osc[index]; }
      IIgsOscillator &operator()(uint index) { return osc[index]; }

      /** Reads an Apple IIGS oscillator list from the given stream. */
      bool read(Common::SeekableReadStream &stream, uint oscillatorCount, bool ignoreAddr = false);
      bool finalize(Common::SeekableReadStream &uint8Wave);
};

00131 struct IIgsInstrumentHeader {
      IIgsEnvelope env;
      uint8 relseg;
      uint8 bendrange;
      uint8 vibdepth;
      uint8 vibspeed;
      IIgsOscillatorList oscList;

      /**
       * Read an Apple IIGS instrument header from the given stream.
       * @param stream The source stream from which to read the data.
       * @param ignoreAddr Should we ignore wave infos' wave address variable's value?
       * @return True if successful, false otherwise.
       */
      bool read(Common::SeekableReadStream &stream, bool ignoreAddr = false);
      bool finalize(Common::SeekableReadStream &uint8Wave);
};

00149 struct IIgsSampleHeader {
      uint16 type;
00151       uint8  pitch; ///< Logarithmic, base is 2**(1/12), unknown multiplier (Possibly in range 1040-1080)
      uint8  unknownByte_Ofs3; // 0x7F in Gold Rush's sound resource 60, 0 in all others.
00153       uint8  volume; ///< Current guess: Logarithmic in 6 dB steps
00154       uint8  unknownByte_Ofs5; ///< 0 in all tested samples.
00155       uint16 instrumentSize; ///< Little endian. 44 in all tested samples. A guess.
00156       uint16 sampleSize; ///< Little endian. Accurate in all tested samples excluding Manhunter I's sound resource 16.
      IIgsInstrumentHeader instrument;

      /**
       * Read an Apple IIGS AGI sample header from the given stream.
       * @param stream The source stream from which to read the data.
       * @return True if successful, false otherwise.
       */
      bool read(Common::SeekableReadStream &stream);
      bool finalize(Common::SeekableReadStream &uint8Wave);
};

00168 struct IIgsChannelInfo {
00169       const IIgsInstrumentHeader *ins; ///< Instrument info
00170       const int8 *relocatedSample; ///< Source sample data (8-bit signed format) using relocation
00171       const int8 *unrelocatedSample; ///< Source sample data (8-bit signed format) without relocation
00172       frac_t pos;     ///< Current sample position
00173       frac_t posAdd;  ///< Current sample position adder (Calculated using note, vibrato etc)
00174       uint8 origNote; ///< The original note without the added relative pitch
00175       frac_t note;    ///< Note (With the added relative pitch)
00176       frac_t vol;     ///< Current volume (Takes both channel volume and enveloping into account)
00177       frac_t chanVol; ///< Channel volume
00178       frac_t startEnvVol; ///< Starting envelope volume
00179       frac_t envVol;  ///< Current envelope volume
00180       uint   envSeg;  ///< Current envelope segment
00181       uint   size;    ///< Sample size
00182       bool   loop;    ///< Should we loop the sample?
00183       bool   end;     ///< Has the playing ended?

      void rewind(); ///< Rewinds the sound playing on this channel to its start
      void setChannelVolume(uint8 volume); ///< Sets the channel volume
      void setInstrument(const IIgsInstrumentHeader *instrument, const int8 *sample); ///< Sets the instrument to be used on this channel
      void noteOn(uint8 noteParam, uint8 velocity); ///< Starts playing a note on this channel
      void noteOff(uint8 velocity); ///< Releases the note on this channel
      void stop(); ///< Stops the note playing on this channel instantly
      bool playing(); ///< Is there a note playing on this channel?
};

00194 class IIgsMidi : public AgiSound {
public:
      IIgsMidi(uint8 *data, uint32 len, int resnum, SoundMgr &manager);
      ~IIgsMidi() { if (_data != NULL) free(_data); }
      virtual uint16 type() { return _type; }
      virtual const uint8 *getPtr() { return _ptr; }
      virtual void setPtr(const uint8 *ptr) { _ptr = ptr; }
      virtual void rewind() { _ptr = _data + 2; _midiTicks = _soundBufTicks = 0; }
protected:
00203       uint8 *_data; ///< Raw sound resource data
00204       const uint8 *_ptr; ///< Pointer to the current position in the MIDI data
00205       uint32 _len; ///< Length of the raw sound resource
00206       uint16 _type; ///< Sound resource type
public:
00208       uint _midiTicks; ///< MIDI song position in ticks (1/60ths of a second)
00209       uint _soundBufTicks; ///< Sound buffer position in ticks (1/60ths of a second)
};

00212 class IIgsSample : public AgiSound {
public:
      IIgsSample(uint8 *data, uint32 len, int resnum, SoundMgr &manager);
      ~IIgsSample() { delete[] _sample; }
      virtual uint16 type() { return _header.type; }
      const IIgsSampleHeader &getHeader() const { return _header; }
      const int8 *getSample() const { return _sample; }
protected:
00220       IIgsSampleHeader _header; ///< Apple IIGS AGI sample header
00221       int8 *_sample;           ///< Sample data (8-bit signed format)
};

/** Apple IIGS MIDI program change to instrument number mapping. */
00225 struct MidiProgramChangeMapping {
00226       byte midiProgToInst[44]; ///< Lookup table for the MIDI program number to instrument number mapping
00227       byte undefinedInst; ///< The undefined instrument number

      // Maps the MIDI program number to an instrument number
      byte map(uint midiProg) const {
            return midiProg < ARRAYSIZE(midiProgToInst) ? midiProgToInst[midiProg] : undefinedInst;
      }
};

/** Apple IIGS AGI instrument set information. */
00236 struct InstrumentSetInfo {
00237       uint byteCount;          ///< Length of the whole instrument set in bytes
00238       uint instCount;          ///< Amount of instrument in the set
00239       const char *md5;         ///< MD5 hex digest of the whole instrument set
00240       const char *waveFileMd5; ///< MD5 hex digest of the wave file (i.e. the sample data used by the instruments)
00241       const MidiProgramChangeMapping &progToInst; ///< Program change to instrument number mapping
};

/** Apple IIGS AGI executable file information. */
00245 struct IIgsExeInfo {
00246       enum AgiGameID gameid;            ///< Game ID
00247       const char *exePrefix;            ///< Prefix of the Apple IIGS AGI executable (e.g. "SQ", "PQ", "KQ4" etc)
00248       uint agiVer;                      ///< Apple IIGS AGI version number, not strictly needed
00249       uint exeSize;                     ///< Size of the Apple IIGS AGI executable file in bytes
00250       uint instSetStart;                ///< Starting offset of the instrument set inside the executable file
00251       const InstrumentSetInfo &instSet; ///< Information about the used instrument set
};

00254 class IIgsMidiChannel {
public:
      IIgsMidiChannel() : _instrument(0), _sample(0), _volume(0) {}
      uint activeSounds() const; ///< How many active sounds are playing?
      void setInstrument(const IIgsInstrumentHeader *instrument, const int8 *sample);
      void setVolume(uint8 volume);
      void noteOff(uint8 note, uint8 velocity);
      void noteOn(uint8 note, uint8 velocity);
      void stopSounds(); ///< Clears the channel of any sounds
      void removeStoppedSounds(); ///< Removes all stopped sounds from this MIDI channel
public:
      typedef Common::Array<IIgsChannelInfo>::const_iterator const_iterator;
      typedef Common::Array<IIgsChannelInfo>::iterator iterator;
00267       Common::Array<IIgsChannelInfo> _gsChannels;     ///< Apple IIGS channels playing on this MIDI channel
protected:
00269       const IIgsInstrumentHeader *_instrument;  ///< Instrument used on this MIDI channel
00270       const int8 *_sample;                                  ///< Sample data used on this MIDI channel
00271       uint8 _volume;                                              ///< MIDI controller number 7 (Volume)
};

00274 class SoundGen2GS : public SoundGen, public Audio::AudioStream {
public:
      SoundGen2GS(AgiEngine *vm, Audio::Mixer *pMixer);
      ~SoundGen2GS();

      void play(int resnum);
      void stop(void);

      // AudioStream API
      int readBuffer(int16 *buffer, const int numSamples);

00285       bool isStereo() const {
            return false;
      }

00289       bool endOfData() const {
            return false;
      }

00293       int getRate() const {
            // FIXME: Ideally, we should use _sampleRate.
            return 22050;
      }

private:
      bool _disabledMidi;
      int _playingSound;
      bool _playing;

      int16 *_sndBuffer;

/**
 * Class for managing Apple IIGS sound channels.
 * TODO: Check what instruments are used by default on the MIDI channels
 * FIXME: Some instrument choices sound wrong
 */
private:
00311       typedef Common::Array<IIgsMidiChannel>::const_iterator const_iterator;
      typedef Common::Array<IIgsMidiChannel>::iterator iterator;
00313       static const uint kSfxMidiChannel = 0; ///< The MIDI channel used for playing sound effects

      bool loadInstruments();
      const IIgsExeInfo *getIIgsExeInfo(enum AgiGameID gameid) const;

      void setProgramChangeMapping(const MidiProgramChangeMapping *mapping);
      bool loadInstrumentHeaders(const Common::FSNode &exePath, const IIgsExeInfo &exeInfo);
      bool loadWaveFile(const Common::FSNode &wavePath, const IIgsExeInfo &exeInfo);

      // Miscellaneous methods
      void fillAudio(int16 *stream, uint len);
      uint32 mixSound();
      void playSound();
      uint activeSounds() const; ///< How many active sounds are playing?
      void stopSounds(); ///< Stops all sounds
      void removeStoppedSounds(); ///< Removes all stopped sounds from the MIDI channels

      // For playing Apple IIGS AGI samples (Sound effects etc)
      bool playSampleSound(const IIgsSampleHeader &sampleHeader, const int8 *sample);
      void playMidiSound();
      void playSampleSound();

      // MIDI commands
      void midiNoteOff(uint8 channel, uint8 note, uint8 velocity);
      void midiNoteOn(uint8 channel, uint8 note, uint8 velocity);
      void midiController(uint8 channel, uint8 controller, uint8 value);
      void midiProgramChange(uint8 channel, uint8 program);
      void midiPitchWheel(uint8 wheelPos);
      //protected:
      const IIgsInstrumentHeader* getInstrument(uint8 program) const;
      //public:
00344       Common::Array<IIgsMidiChannel> _midiChannels;         ///< Information about each MIDI channel
      //protected:
00346       Common::Array<int8> _wave;                                        ///< Sample data used by the Apple IIGS MIDI instruments
00347       const MidiProgramChangeMapping *_midiProgToInst;      ///< MIDI program change to instrument number mapping
00348       Common::Array<IIgsInstrumentHeader> _instruments;     ///< Instruments used by the Apple IIGS AGI
};

} // End of namespace Agi

#endif /* AGI_SOUND_2GS_H */

Generated by  Doxygen 1.6.0   Back to index