Skip to content

Commit

Permalink
stubbing out the command prompt modes
Browse files Browse the repository at this point in the history
  • Loading branch information
bbbradsmith committed Apr 28, 2024
1 parent 34036b3 commit 89684a6
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 31 deletions.
99 changes: 73 additions & 26 deletions cmd/cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ int platform_argc();
const char* platform_argv(int index); // note: return only valid until next argv/getenv
const char* platform_getenv(const char* name); // note: return only valid until next argv/getenv, name limit of 63 chars
FILE* platform_fopen(const char* path, const char* mode);
bool platform_kbhit();

// unit testing (unit_test.cpp)
int unit_test(const char* path);

//
// global data
Expand Down Expand Up @@ -52,7 +56,7 @@ void fatal_log(const char* msg)
// load files
//

void* load_file(const char* path, const char* mode, size_t& filesize, bool silent_notfound=false)
static void* load_file(const char* path, const char* mode, size_t& filesize, bool silent_notfound=false)
{
filesize = 0;
// open file
Expand Down Expand Up @@ -89,7 +93,7 @@ void* load_file(const char* path, const char* mode, size_t& filesize, bool silen
return fd;
}

bool load_ini(const char* path, bool silent_notfound=false)
static bool load_ini(const char* path, bool silent_notfound=false)
{
size_t filesize;
char* ini = reinterpret_cast<char*>(load_file(path,"rt",filesize,silent_notfound));
Expand Down Expand Up @@ -124,32 +128,19 @@ static bool parse_i64(const char* s, int64_t& v)
return true;
}

inline static bool key_match(const char* key_test, int len, const char* key_reference)
{
if (len < 0) len = 256; // greater than maximum possible key_reference length
while ((len==0 || *key_test != 0) && *key_reference != 0)
{
char c = *key_test;
if (c >= 'a' && c <= 'z') c = (c - 'a') + 'A';
if (c != *key_reference) return false;
++key_test;
++key_reference;
--len;
}
return (*key_reference == 0);
}

int32_t parse_chan(const char* s, bool ch[NSF_CHANNEL_COUNT])
static int32_t parse_chan(const char* s, bool ch[NSF_CHANNEL_COUNT])
{
for (int32_t i=0; i<NSF_CHANNEL_COUNT; ++i)
{
const char* key = nsfplay_channel_info(core,i).key;
const char* sm = s;
while (*key && *sm)
{
char c = *sm;
if (c >= 'a' && c <= 'z') c = (c - 'a') + 'A';
if (c != *key) break;
char ck = *key;
char cs = *sm;
if (ck >= 'a' && ck <= 'z') ck = (ck - 'a') + 'A';
if (cs >= 'a' && cs <= 'z') cs = (cs - 'a') + 'A';
if (ck != cs) break;
}
if (*key == 0 && *sm == 0)
{
Expand All @@ -164,12 +155,12 @@ struct
{
int input = -1;
int waveout = -1;
int waveout_multi = -1;
int unit_test = -1;
int save_default_ini = -1;
int32_t track = 0;
int32_t fade = -1;
int64_t time = -1;
bool waveout_multi = false;
bool help = false;
bool solo[NSF_CHANNEL_COUNT] = {0};
bool mute[NSF_CHANNEL_COUNT] = {0};
Expand All @@ -180,12 +171,12 @@ int parse_commandline() // returns -1 on success, otherwise is index of bad argu
bool implied_ini = true;
arg.input = -1;
arg.waveout = -1;
arg.waveout_multi = -1;
arg.unit_test = -1;
arg.save_default_ini = -1;
arg.track = 0;
arg.fade = -1;
arg.time = -1;
arg.waveout_multi = false;
arg.help = false;
std::memset(arg.solo,0,sizeof(arg.solo));
std::memset(arg.mute,0,sizeof(arg.mute));
Expand Down Expand Up @@ -216,7 +207,7 @@ int parse_commandline() // returns -1 on success, otherwise is index of bad argu
case 'i': ++i; { if (arg.input >= 0) return i; } arg.input = i; break;
case 't': ++i; if (!parse_i32(platform_argv(i),arg.track)) return i; break;
case 'w': ++i; arg.waveout = i; break;
case 'v': arg.waveout_multi = true; break;
case 'v': ++i; arg.waveout_multi = i; break;
case 'd': ++i; arg.save_default_ini = i; break;
case 'a': ++i; implied_ini = false; break; // any commandline ini disables the implied ini
case 'n': implied_ini = false; break;
Expand Down Expand Up @@ -259,7 +250,7 @@ int parse_commandline() // returns -1 on success, otherwise is index of bad argu
case 'i': ++i; break;
case 't': ++i; break;
case 'w': ++i; break;
case 'v': break;
case 'v': ++i; break;
case 'd': ++i; break;
case 'a': ++i; if(!load_ini(platform_argv(i))) return i; break;
case 'n': break;
Expand Down Expand Up @@ -292,7 +283,7 @@ const char HELP_TEXT[] =
" -i file set input file (useful if filename begins with -)\n"
" -t num set starting track (1-256)\n"
" -w file wave output to file\n"
" -v wave output all tracks in file using TITLE_FORMAT as filename\n"
" -v path wave output all tracks in path, appending TITLE_FORMAT + .wav as filename\n"
" -d file create a new INI file with default settings\n"
" -a file apply INI file (implies -n)\n"
" -n don't automatically load the default INI\n"
Expand Down Expand Up @@ -326,6 +317,22 @@ void help_chans()
printf("\n");
}

//
// WAVE file output
//

static bool waveout(const char* path)
{
(void)path;
// TODO
// open file, print error and return fale if failed
// print song info brief
// print a progress bar [----]\r
// fill in the progress one character at a time (so a log will just make 2 lines)
// fixup WAV header for file length
return false;
}

//
// main
//
Expand Down Expand Up @@ -374,6 +381,12 @@ int run()
return 0;
}

// unit test
if (arg.unit_test >= 0)
{
return unit_test(platform_argv(arg.unit_test));
}

// load input file
if (arg.input >= 0)
{
Expand All @@ -382,10 +395,44 @@ int run()
nsfplay_load(core,fd,uint32_t(fs));
std::free(fd);
}
// TODO print NSF info brief

// set track if requested
if (arg.track > 0) nsfplay_song(core,uint8_t(arg.track-1));

// WAV file output
if (arg.waveout)
{
if (!waveout(platform_argv(arg.waveout))) return -1;
std::printf("Success.\n");
return 0;
}
if (arg.waveout_multi)
{
if (nsfplay_song_count(core) < 1)
{
std::fprintf(stderr,"No songs in input file.\n");
return -1;
}
for (int32_t i=0; i<nsfplay_song_count(core); ++i)
{
nsfplay_song(core,uint8_t(i));
const int PATH_SIZE = 2048;
char path[PATH_SIZE];
::strcpy_s(path,PATH_SIZE,platform_argv(arg.waveout_multi));
::strcat_s(path,PATH_SIZE,nsfplay_prop_str(core,NSF_PROP_SONG_TITLE));
::strcat_s(path,PATH_SIZE,".wav");
if (!waveout(path)) return -1;
}
std::printf("Success.\n");
return 0;
}

// TODO regular playback with prompt
// print song brief
// enter prompt with playback loop (if autoplay, otherwise just prompt)
// prompt is a branching menu

// TODO the rest of this function is test code I am keeping as an example for later menus

/*
Expand Down
1 change: 1 addition & 0 deletions cmd/cmd.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
<ItemGroup>
<ClCompile Include="cmd.cpp" />
<ClCompile Include="platform.cpp" />
<ClCompile Include="unit_test.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
1 change: 1 addition & 0 deletions cmd/cmd.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
<ItemGroup>
<ClCompile Include="cmd.cpp" />
<ClCompile Include="platform.cpp" />
<ClCompile Include="unit_test.cpp" />
</ItemGroup>
</Project>
12 changes: 12 additions & 0 deletions cmd/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <shellapi.h> // GetCommandLineW, CommandLineToArgW
#include <cstdlib> // std::malloc, std::free
#include <cstdio> // std::_wfopen
#include <conio.h> // kbhit

static UINT store_cp = 0;
static LPWSTR* store_argv;
Expand Down Expand Up @@ -132,6 +133,11 @@ FILE* platform_fopen(const char* path, const char* mode)
return NULL;
}

bool platform_kbhit()
{
return _kbhit();
}

#else

#include <cstdio> // std::fopen
Expand Down Expand Up @@ -174,4 +180,10 @@ FILE* platform_fopen(const char* path, const char* mode)
return std::fopen(path,mode);
}

bool platform_kbhit()
{
// TODO
return true;
}

#endif
11 changes: 11 additions & 0 deletions cmd/unit_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// unit_test.cpp
// Executes a unit test definition file

#include <nsfplaycore.h>

int unit_test(const char* path)
{
// TODO
(void)path;
return -1;
}
9 changes: 6 additions & 3 deletions core/nsfplaycore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,13 @@ NSFGroupInfo nsfplay_group_info(const NSFCore* core, int32_t group)

int32_t nsfplay_set_enum(const char* key)
{
// no mutex needed, just a static lookup
return NSFCore::set_enum(key);
}

int32_t nsfplay_group_enum(const char* key)
{
// no mutex needed, just a static lookup
return NSFCore::group_enum(key);
}

Expand All @@ -241,14 +243,14 @@ bool nsfplay_load_bin(NSFCore* core, const void* bin_data, uint32_t bin_size, bo
return core->load(reinterpret_cast<const uint8*>(bin_data),bin_size,assume,true);
}

uint32_t nsfplay_song_count(const NSFCore* core)
int32_t nsfplay_song_count(const NSFCore* core)
{
NSF_MUTEX_GUARD();
if (!core) return 0;
return core->prop_int(NSF_PROP_ACTIVE_SONG_COUNT);
}

uint32_t nsfplay_song_current(const NSFCore* core)
int32_t nsfplay_song_current(const NSFCore* core)
{
NSF_MUTEX_GUARD();
if (!core) return 0;
Expand Down Expand Up @@ -457,6 +459,7 @@ uint32_t nsfplay_emu_cycles_to_next_sample(const NSFCore* core)

NSFOpcode nsfplay_emu_opcode(uint8 op)
{
// no mutex needed, just a static lookup
NSF_UNUSED(op);
// TODO
return {0};
Expand Down Expand Up @@ -660,7 +663,7 @@ void nsfplay_cycles_to_time(const NSFCore* core, uint64_t cycles, int32_t* hours

const char* nsfplay_local_text(const NSFCore* core, int32_t textenum)
{
NSF_MUTEX_GUARD();
if (!core) return NSFCore::local_text(textenum,0); // default locale
NSF_MUTEX_GUARD();
return core->local_text(textenum);
}
4 changes: 2 additions & 2 deletions include/nsfplaycore.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ bool nsfplay_load(NSFCore* core, const void* nsf_data, uint32_t nsf_size, bool a
bool nsfplay_load_bin(NSFCore* core, const void* bin_data, uint32_t bin_size, bool assume=false);

// song control
uint32_t nsfplay_song_count(const NSFCore* core); // number of songs in loaded NSF (or NSF playlist if active, see PROP_ACTIVE_PLAYLIST)
uint32_t nsfplay_song_current(const NSFCore* core); // current active song
int32_t nsfplay_song_count(const NSFCore* core); // number of songs in loaded NSF (or NSF playlist if active, see PROP_ACTIVE_PLAYLIST)
int32_t nsfplay_song_current(const NSFCore* core); // current active song
bool nsfplay_song(NSFCore* core, uint8_t song); // set song, false if song out of bounds, automatically calls song_play
void nsfplay_song_play(NSFCore* core); // resets the song and executes its INIT routine
void nsfplay_seek(NSFCore* core, uint64_t samples);
Expand Down

0 comments on commit 89684a6

Please sign in to comment.