firmware of NBCTRLV1 / AYC01

Dependencies:   SDFileSystemEx mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers nlg_mini.cpp Source File

nlg_mini.cpp

00001 
00002 //
00003 // nlg_mini.cpp
00004 //
00005 
00006 #include <stdio.h>
00007 #include <stdlib.h>
00008 #include <string.h>
00009 
00010 #include "nlg_mini.h"
00011 
00012 /* 単位を扱いやすいように */
00013 typedef unsigned char byte;
00014 typedef unsigned short word;
00015 typedef unsigned long dword;
00016 
00017 // 変数読み出し(WORD)
00018 word ReadWORD(byte *p)
00019 {
00020     return
00021         ((word)p[0]) |
00022         ((word)p[1])<<8;
00023 }
00024 
00025 // 変数読み出し(DWORD)
00026 dword ReadDWORD(byte *p)
00027 {
00028     return
00029         ((dword)p[0]) |
00030         ((dword)p[1])<<8 |
00031         ((dword)p[2])<<16 |
00032         ((dword)p[3])<<24;
00033 }
00034 
00035 // NLGファイルを開く
00036 int OpenNLG(NLG_CTX *np, const char *filename)
00037 {
00038     byte hdr[0x60];
00039 
00040     np->fp = fopen(filename, "rb");
00041 
00042     // ファイルが開けない
00043     if (!np->fp)
00044         return NLG_FILEERR;
00045 
00046     // ヘッダ部分の読み込み
00047     fread(hdr, 0x60, 1, np->fp);
00048 
00049     // IDの確認
00050     if (memcmp(hdr, "NLG1", 4) != 0)
00051     {
00052         CloseNLG(np);
00053         return NLG_UNK_FORMAT;
00054     }
00055 
00056     // バージョン
00057     np->version = ReadWORD(hdr + 4);
00058 
00059     // クロック
00060     np->baseclk = ReadDWORD(hdr + 72);
00061 
00062     // ティック
00063     np->tick = ReadDWORD(hdr + 76);
00064     np->tick_us = np->tick / 4;
00065 
00066     // 長さ
00067     np->length = ReadDWORD(hdr + 88);
00068 
00069     // 位置
00070     fseek(np->fp, 0x60, SEEK_SET);
00071 
00072     // CTC初期化
00073     np->ctc0 = np->ctc3 = 0;
00074 
00075     return NLG_OK;
00076 }
00077 
00078 // ファイルを閉じる
00079 void CloseNLG(NLG_CTX *np)
00080 {
00081     if (!np->fp)
00082         return;
00083 
00084     fclose(np->fp);
00085 
00086 #if defined(__MICROLIB) && defined(__ARMCC_VERSION) // with microlib and ARM compiler
00087     free(np->fp);
00088 #endif
00089 
00090     np->fp = NULL;
00091 }
00092 
00093 // データの読み出し
00094 int ReadNLG(NLG_CTX *np)
00095 {
00096     return fgetc(np->fp);
00097 }
00098 
00099 // ファイルポインタの位置を取得
00100 long TellNLG(NLG_CTX *np)
00101 {
00102     return ftell(np->fp);
00103 }
00104 
00105 // ファイルポインタの位置を設定
00106 void SeekNLG(NLG_CTX *np, long pos)
00107 {
00108     fseek(np->fp, pos, SEEK_SET);
00109 }
00110 
00111 // ティック(us)の取得
00112 int GetTickUsNLG(NLG_CTX *np)
00113 {
00114     return np->tick_us;
00115 }
00116 
00117 // Set Tick
00118 // (CTC0 * 64us) * CTC3
00119 static inline void SetTickNLG(NLG_CTX *np)
00120 {
00121     np->tick = ((np->ctc0 * 256) * np->ctc3);
00122     np->tick_us = np->tick / 4;
00123 }
00124 
00125 // CTC0値の設定
00126 void SetCTC0_NLG(NLG_CTX *np, int value)
00127 {
00128     np->ctc0 = value;
00129     SetTickNLG(np);
00130 }
00131 
00132 // CTC3値の設定
00133 void SetCTC3_NLG(NLG_CTX *np, int value)
00134 {
00135     np->ctc3 = value;
00136     SetTickNLG(np);
00137 }
00138 
00139 // 曲の長さを得る
00140 int GetLengthNLG(NLG_CTX *np)
00141 {
00142     return np->length;
00143 }
00144 
00145 // ベースクロックを得る
00146 int GetBaseClkNLG(NLG_CTX *np)
00147 {
00148     return np->baseclk;
00149 }