| 1 |
/* |
| 2 |
* This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>. |
| 3 |
* It is copyright by its individual contributors, as recorded in the |
| 4 |
* project's Git history. See COPYING.txt at the top level for license |
| 5 |
* terms and a link to the Git history. |
| 6 |
*/ |
| 7 |
#ifndef __HMP_H |
| 8 |
#define __HMP_H |
| 9 |
|
| 10 |
#include <array> |
| 11 |
#include <memory> |
| 12 |
#include <tuple> |
| 13 |
#include <vector> |
| 14 |
#ifdef _WIN32 |
| 15 |
#include <windows.h> |
| 16 |
#include <mmsystem.h> |
| 17 |
#endif |
| 18 |
#include "physfsx.h" |
| 19 |
|
| 20 |
namespace dcx { |
| 21 |
|
| 22 |
#define HMP_TRACKS 32 |
| 23 |
#ifdef _WIN32 |
| 24 |
#define MIDI_VOLUME_SCALE 128 |
| 25 |
#define HMP_BUFFERS 4 |
| 26 |
#define HMP_BUFSIZE 1024 |
| 27 |
#define HMP_INVALID_FILE -1 |
| 28 |
#define HMP_OUT_OF_MEM -2 |
| 29 |
#define HMP_MM_ERR -3 |
| 30 |
#define HMP_EOF 1 |
| 31 |
|
| 32 |
#define MIDI_CONTROL_CHANGE 0xB |
| 33 |
#define MIDI_PROGRAM_CHANGE 0xC |
| 34 |
|
| 35 |
#define MIDI_BANK_SELECT_MSB 0x00 |
| 36 |
#define MIDI_BANK_SELECT_LSB 0x20 |
| 37 |
#define MIDI_VOLUME 0x07 |
| 38 |
#define MIDI_PANPOT 0x0A |
| 39 |
#define MIDI_REVERB 0x5B |
| 40 |
#define MIDI_CHORUS 0x5D |
| 41 |
#define MIDI_ALL_SOUNDS_OFF 0x78 |
| 42 |
#define MIDI_RESET_ALL_CONTROLLERS 0x79 |
| 43 |
#define MIDI_ALL_NOTES_OFF 0x7B |
| 44 |
|
| 45 |
#define HMP_LOOP_START 0x6E |
| 46 |
#define HMP_LOOP_END 0x6F |
| 47 |
#endif |
| 48 |
|
| 49 |
#ifdef _WIN32 |
| 50 |
struct event |
| 51 |
{ |
| 52 |
unsigned int delta; |
| 53 |
unsigned char msg[3]; |
| 54 |
unsigned char *data; |
| 55 |
unsigned int datalen; |
| 56 |
}; |
| 57 |
#endif |
| 58 |
|
| 59 |
struct hmp_track |
| 60 |
{ |
| 61 |
std::unique_ptr<uint8_t[]> data; |
| 62 |
unsigned char *loop; |
| 63 |
unsigned int len; |
| 64 |
unsigned char *cur; |
| 65 |
unsigned int left; |
| 66 |
unsigned int cur_time; |
| 67 |
unsigned int loop_start; |
| 68 |
int loop_set; |
| 69 |
}; |
| 70 |
|
| 71 |
struct hmp_file |
| 72 |
{ |
| 73 |
~hmp_file(); |
| 74 |
PHYSFS_sint64 filesize; |
| 75 |
int num_trks; |
| 76 |
std::array<hmp_track, HMP_TRACKS> trks; |
| 77 |
unsigned int cur_time; |
| 78 |
unsigned int loop_start; |
| 79 |
unsigned int loop_end; |
| 80 |
int looping; |
| 81 |
int tempo; |
| 82 |
#ifdef _WIN32 |
| 83 |
MIDIHDR *evbuf; |
| 84 |
HMIDISTRM hmidi; |
| 85 |
UINT devid; |
| 86 |
#endif |
| 87 |
unsigned char *pending; |
| 88 |
unsigned int pending_size; |
| 89 |
unsigned int pending_event; |
| 90 |
int stop; |
| 91 |
int bufs_in_mm; |
| 92 |
int bLoop; |
| 93 |
unsigned int midi_division; |
| 94 |
#ifdef _WIN32 |
| 95 |
std::array<MIDIHDR, HMP_BUFFERS> midi_headers; |
| 96 |
std::array<std::array<char, HMP_BUFSIZE>, HMP_BUFFERS> midi_buffers; |
| 97 |
#endif |
| 98 |
}; |
| 99 |
|
| 100 |
enum class hmp_open_error : uint8_t |
| 101 |
{ |
| 102 |
None, |
| 103 |
/* All PhysFS errors must be before any non-PhysFS errors. The error |
| 104 |
* printing code relies on this to determine which error numbers are |
| 105 |
* physfs_* and which are not. |
| 106 |
*/ |
| 107 |
physfs_open, |
| 108 |
physfs_read_header, |
| 109 |
physfs_seek0, |
| 110 |
physfs_read_tracks, |
| 111 |
physfs_seek1, |
| 112 |
physfs_read_tempo, |
| 113 |
physfs_seek2, |
| 114 |
physfs_read_track_length, |
| 115 |
physfs_read_track_data, |
| 116 |
/* Non-PhysFS reasons start here */ |
| 117 |
content_header, |
| 118 |
track_count, |
| 119 |
}; |
| 120 |
|
| 121 |
/* PHYSFS_ErrorCode member is only meaningful if hmp_open_error is a |
| 122 |
* hmp_open_error::physfs_* code. Otherwise, it is formally undefined. |
| 123 |
* Informally, it is 0. */ |
| 124 |
using hmp_open_result = std::tuple<std::unique_ptr<hmp_file>, hmp_open_error, PHYSFS_ErrorCode>; |
| 125 |
using hmpmid_result = std::tuple<std::vector<uint8_t>, hmp_open_error>; |
| 126 |
|
| 127 |
hmp_open_result hmp_open(const char *filename); |
| 128 |
hmpmid_result hmp2mid(const char *hmp_name); |
| 129 |
#ifdef _WIN32 |
| 130 |
void hmp_setvolume(hmp_file *hmp, int volume); |
| 131 |
int hmp_play(hmp_file *hmp, int bLoop); |
| 132 |
void hmp_pause(hmp_file *hmp); |
| 133 |
void hmp_resume(hmp_file *hmp); |
| 134 |
void hmp_reset(); |
| 135 |
#endif |
| 136 |
|
| 137 |
} |
| 138 |
#endif |