From 1a88401f49c42471827935c015472dbefd9bd06b Mon Sep 17 00:00:00 2001 From: qishipai <88886376+qishipai@users.noreply.github.com> Date: Sat, 13 Nov 2021 01:08:32 +0800 Subject: [PATCH] Add files via upload --- Nlist.cxx | 39 ++++++++++++++++++++++++++------------- Nlist.hxx | 14 +++++++------- player.cxx | 48 +++++++++++++++++++++++++++++------------------- 3 files changed, 62 insertions(+), 39 deletions(-) diff --git a/Nlist.cxx b/Nlist.cxx index 5154bb2..956bfa9 100644 --- a/Nlist.cxx +++ b/Nlist.cxx @@ -10,29 +10,42 @@ NVnote::NVnote(double T, const NVseq_event &E): Tstart(T), Tend(114514191981.0), track(E.track), channel(E.chan), key(E.num), vel(E.value) { } -NVnoteList::NVnoteList(const char *name): - Tread(0.0), abstick(0), keys(nullptr) +bool NVnoteList::start_parse(const char *name) { - if ((err = !M.mid_open(name))) + if (!M.mid_open(name)) { - return; + return false; } - print(name, "类型码: %4hu\n", M.type); - print(name, "轨道数: %4hu\n", M.tracks); - print(name, "分辨率: %4hu\n", M.ppnq); +#if defined(_WIN32) || defined(_WIN64) - if ((err = M.type == 2)) + print("INFO", "类型码: %4hu\n", M.type ); + print("INFO", "轨道数: %4hu\n", M.tracks); + print("INFO", "分辨率: %4hu\n", M.ppnq ); + +#else + + print("INFO", "类型码: \e[35m%4hu\e[m\n", M.type ); + print("INFO", "轨道数: \e[33m%4hu\e[m\n", M.tracks); + print("INFO", "分辨率: \e[36m%4hu\e[m\n", M.ppnq ); + +#endif + + Tread = 0.0; abstick = 0; + + if (M.type == 2) { - error("Nlist", "%s: MIDI格式不支持!(type2)\n"); - return; + M.mid_close(); + error("Nlist", "%s: 类型不支持!(type=2)\n"); + return false; } S.seq_init(M); dT = 0.5 / M.ppnq; keys = new rmPR::t [M.tracks]; + return true; } -NVnoteList::~NVnoteList() +void NVnoteList::destroy_all() { delete[] keys; keys = nullptr; M.mid_close(); S.seq_destroy(); @@ -59,7 +72,7 @@ void NVnoteList::update_to(double T) u32_t speed = *E.data; (speed <<= 8) |= *(E.data + 1); (speed <<= 8) |= *(E.data + 2); - dT = 1e-6 * speed / M.ppnq; + dT = 0.000001 * speed / M.ppnq; } break; @@ -89,7 +102,7 @@ void NVnoteList::update_to(double T) } } -void NVnoteList::OR() +void NVnoteList::OR() // 大概有用吧 { for (int i = 0; i < 128; ++i) { diff --git a/Nlist.hxx b/Nlist.hxx index 7a69cd3..882cc1a 100644 --- a/Nlist.hxx +++ b/Nlist.hxx @@ -18,20 +18,20 @@ class NVnoteList // 音符队列 { public: - NVmidiFile M; // MIDI文件 - bool err; // 错误标志 - double Tread; // 当前读取位置(秒) - std::list L[128]; // 音符列表 + NVmidiFile M; // MIDI文件 + double Tread; // 当前读取位置(秒) + std::list L[128]; // 音符列表 /* 打开一个MIDI文件 */ - NVnoteList(const char *name); + bool start_parse(const char *name); - ~NVnoteList(); + /* 关闭组件 */ + void destroy_all(); /* 将第T秒前的音符放入列表 */ void update_to(double T); - void OR(); // 懂BM的都懂,不懂的用不到 + void OR(); // 懂BM的都懂,不懂的用不到 /* 移除列表中第T秒前的音符 */ void remove_to(double T); diff --git a/player.cxx b/player.cxx index 069b651..ae84f20 100644 --- a/player.cxx +++ b/player.cxx @@ -10,7 +10,7 @@ using namespace NV; using namespace std; -NVnoteList *MIDI; +NVnoteList MIDI; Canvas *Win; Canvas::color *Col; @@ -19,7 +19,7 @@ HSTREAM Stm; SDL_Surface *Scr; -int _WinH, pps = 4000; +int _WinH, pps = 1000; double Tplay = 0.0, Tscr; static void DrawNote(u16_t k, const NVnote &n) @@ -81,6 +81,16 @@ BOOL CALLBACK filter(HSTREAM S, DWORD trk, BASS_MIDI_EVENT *E, BOOL sk, void *u) return TRUE; } +#ifndef __ANDROID__ +#define NVmain main +#endif + +#if defined(_WIN32) || defined(_WIN64) +#define BASSMIDI_LIB "bassmidi.dll" +#else +#define BASSMIDI_LIB "libbassmidi.so" +#endif + int NVmain(int ac, char **av) { if (ac != 3) @@ -89,27 +99,24 @@ int NVmain(int ac, char **av) return 1; } - Win = new Canvas; MIDI = new NVnoteList(av[1]); - - if (MIDI->err) + if (!MIDI.start_parse(av[1])) { - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, + Canvas C; SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error!!!!!", "MIDI File Load Failed", nullptr); - delete MIDI; delete Win; return 1; } - Col = new Canvas::color [MIDI->M.tracks]; + Win = new Canvas; Col = new Canvas::color [MIDI.M.tracks]; - for (int i = 0; i < MIDI->M.tracks; ++i) + for (int i = 0; i < MIDI.M.tracks; ++i) { - float ang = 6.283185307 * i / (MIDI->M.tracks + 1) * 4; + float ang = 6.283185307 * i / (MIDI.M.tracks + 1) * 4; Col[i] = complex(cosf(ang), sinf(ang)) * 512.0f; } _WinH = Win->WinH - Win->TH; Tscr = (double)_WinH / pps; - BASS_PluginLoad("libbassmidi.so", 0); + BASS_PluginLoad(BASSMIDI_LIB, 0); BASS_SetConfig(BASS_CONFIG_MIDI_AUTOFONT, 0); BASS_Init(-1, 44100, 0, 0, nullptr); @@ -131,13 +138,13 @@ int NVmain(int ac, char **av) while (BASS_ChannelIsActive(Stm) != BASS_ACTIVE_STOPPED) { - MIDI->update_to(Tplay + Tscr); - MIDI->remove_to(Tplay); - MIDI->OR(); Win->canvas_clear(); + MIDI.update_to(Tplay + Tscr); + MIDI.remove_to(Tplay); + MIDI.OR(); Win->canvas_clear(); for (int k = 0; k < 128; ++k) { - for (const NVnote &n : MIDI->L[KeyMap[k]]) + for (const NVnote &n : MIDI.L[KeyMap[k]]) { DrawNote(k, n); } @@ -150,15 +157,18 @@ int NVmain(int ac, char **av) Tplay = BASS_ChannelBytes2Seconds(Stm, BASS_ChannelGetPosition(Stm, BASS_POS_BYTE)); } - BASS_Free(); BASS_PluginFree(0); - delete MIDI; delete Win; delete[] Col; + delete Win; delete[] Col; + BASS_Free(); BASS_PluginFree(0); MIDI.destroy_all(); return 0; } +#ifdef __ANDROID__ + int main(int ac, char **av) { - char midi[] = "/sdcard/default.mid"; - char sf [] = "/sdcard/default.sf2"; + char midi[] = "/sdcard/default.mid", sf[] = "/sdcard/default.sf2"; char *arg[4]{nullptr, midi, sf, nullptr}; return NVmain(3, arg); } + +#endif