Skip to content

Commit

Permalink
Add library build
Browse files Browse the repository at this point in the history
  • Loading branch information
ds-sloth committed Oct 29, 2024
1 parent 95b2b9d commit b70316e
Show file tree
Hide file tree
Showing 10 changed files with 207 additions and 148 deletions.
13 changes: 9 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ project(spc2it)
set(spc2it_sources
src/emu.c
src/it.c
src/main.c
src/sound.c
src/spc700.c
src/lib.c

src/emu.h
src/it.h
Expand All @@ -15,7 +15,12 @@ set(spc2it_sources
src/spc2ittypes.h
)

add_executable(spc2it ${spc2it_sources})
target_include_directories(spc2it PRIVATE src)
add_library(libspc2it STATIC ${spc2it_sources})
target_include_directories(libspc2it PRIVATE src)
target_include_directories(libspc2it PUBLIC include)
target_link_libraries(libspc2it m)

add_executable(spc2it_cli src/main.c)
target_link_libraries(spc2it_cli libspc2it)
set_target_properties(spc2it_cli PROPERTIES OUTPUT_NAME "spc2it")

target_link_libraries(spc2it m)
18 changes: 18 additions & 0 deletions include/spc2it.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/****************************************************
*Part of SPC2IT, read readme.md for more information*
****************************************************/

#ifndef SPC2IT_H
#define SPC2IT_H

#ifdef __cplusplus
extern "C" {
#endif

int spc2it_convert(const char* src, const char* dest);

#ifdef __cplusplus
} // extern "C"
#endif

#endif
4 changes: 2 additions & 2 deletions src/emu.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void (*SPC_Write_DSP_Hook)(u8); // function pointer
s32 SPCtime;
SPCFileInformation SPCInfo;

static s32 LoadZState(char *fn)
static s32 LoadZState(const char *fn)
{
SPCFile *sFile = (SPCFile *)calloc(1, sizeof(SPCFile));
if (sFile == NULL)
Expand Down Expand Up @@ -95,7 +95,7 @@ static s32 LoadZState(char *fn)

// PUBLIC (non-static) functions

s32 SPCInit(char *fn)
s32 SPCInit(const char *fn)
{
Reset_SPC();
if (LoadZState(fn))
Expand Down
2 changes: 1 addition & 1 deletion src/emu.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extern void (*SPC_Write_DSP_Hook)(u8);

#define SPCUpdateRate 100

s32 SPCInit(char *);
s32 SPCInit(const char *);
void SPCAddWriteDSPCallback(void (*ToAddCallback)(u8));

#endif
35 changes: 14 additions & 21 deletions src/it.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,15 +346,12 @@ s32 ITUpdate() // Dumps pattern buffers to file
return 0;
}

s32 ITWrite(char *fn) // Write the final IT file
s32 ITWrite(const char *fn) // Write the final IT file
{
s32 ret = 0;

if (fn == NULL)
{
// printf("Error: no IT filename\n");
ret = 1;
goto cleanup;
return 1;
}

FILE *f;
Expand All @@ -365,8 +362,7 @@ s32 ITWrite(char *fn) // Write the final IT file
if (pInfo == NULL)
{
// printf("Error: could not allocate memory for ITPatternInfo struct\n");
ret = 1;
goto cleanup;
return 1;
}

// START IT CLEANUP
Expand All @@ -390,19 +386,15 @@ s32 ITWrite(char *fn) // Write the final IT file

ITpattlen[ITcurbuf++] = ITbufpos;
if (ITUpdate()) // Save the changes we just made
{
ret = 1;
goto cleanup;
}
return 1;
// END IT CLEANUP

ITFileHeader *fHeader;
fHeader = (ITFileHeader *)calloc(1, sizeof(ITFileHeader));
if (fHeader == NULL)
{
// printf("Error: could not allocate memory for ITFileHeader struct\n");
ret = 1;
goto cleanup;
return 1;
}

memcpy(fHeader->magic, "IMPM", 4);
Expand Down Expand Up @@ -436,8 +428,7 @@ s32 ITWrite(char *fn) // Write the final IT file
{
// printf("Error: could not open IT file\n");
free(fHeader);
ret = 1;
goto cleanup;
return 1;
}

// header
Expand Down Expand Up @@ -471,9 +462,8 @@ s32 ITWrite(char *fn) // Write the final IT file
{
if (ITSSave(ITSamples[i], f))
{
ret = 1;
fclose(f);
goto cleanup;
return 1;
}
}

Expand All @@ -483,15 +473,19 @@ s32 ITWrite(char *fn) // Write the final IT file
// close file
fclose(f);

cleanup:
return 0;
}

void ITCleanup()
{
// generic cleanup that must occur in any case
for (i = 0; i < NUM_PATT_BUFS; i++)
for (int i = 0; i < NUM_PATT_BUFS; i++)
{
free(ITpattbuf[i]);
ITpattbuf[i] = NULL;
}

for (i = 0; i < numsamps; i++)
for (int i = 0; i < IT_SAMPLE_MAX; i++)
{
if (!ITSamples[i])
continue;
Expand All @@ -504,7 +498,6 @@ s32 ITWrite(char *fn) // Write the final IT file

free(ITPatterns);
ITPatterns = NULL;
return ret;
}

int ITMix()
Expand Down
3 changes: 2 additions & 1 deletion src/it.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

s32 ITStart(s32); // Opens temp file, inits writing
s32 ITUpdate(); // Dumps pattern buffers to file
s32 ITWrite(char *fn); // Stops recording and writes IT file from temp data
s32 ITWrite(const char *fn); // Stops recording and writes IT file from temp data
void ITCleanup(); // clears IT temp data
int ITMix();

// Macros
Expand Down
138 changes: 138 additions & 0 deletions src/lib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/****************************************************
*Part of SPC2IT, read readme.md for more information*
****************************************************/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <limits.h>
#include <stdbool.h>

#include "sound.h"
#include "it.h"
#include "emu.h"
#include "sneese_spc.h"

_Static_assert(sizeof(u8) == 1, "Warning: wrong size u8");
_Static_assert(sizeof(u16) == 2, "Warning: wrong size u16");
_Static_assert(sizeof(u32) == 4, "Warning: wrong size u32");
_Static_assert(sizeof(u64) == 8, "Warning: wrong size u64");
_Static_assert(sizeof(s8) == 1, "Warning: wrong size s8");
_Static_assert(sizeof(s16) == 2, "Warning: wrong size s16");
_Static_assert(sizeof(s32) == 4, "Warning: wrong size s32");
_Static_assert(sizeof(s64) == 8, "Warning: wrong size s64");
_Static_assert(sizeof(ITFileHeader) == 192, "Warning: wrong size ITFileHeader");
_Static_assert(sizeof(ITFileSample) == 80, "Warning: wrong size ITFileSample");
_Static_assert(sizeof(ITFilePattern) == 8, "Warning: wrong size ITFilePattern");
_Static_assert(sizeof(SPCFile) == 65920, "Warning: wrong size SPCFile");

int spc2it_load(const char* src, int ITrows, int limit, int verbose)
{
if (ITStart(ITrows))
{
// printf("Error: failed to initialize pattern buffers\n");
return(1);
}
if (SPCInit(src)) // Reset SPC and load state
{
// printf("Error: failed to initialize emulation\n");
return(1);
}
if (SNDInit())
{
// printf("Error: failed to initialize sound\n");
return(1);
}

if ((!limit) && (SPCtime))
limit = SPCtime;
else if (!limit)
limit = 60;

if (verbose)
{
printf("Time (seconds): %i\n", limit);

printf("IT Parameters:\n");
printf(" Rows/pattern: %d\n", ITrows);

printf("ID info:\n");
printf(" Song: %s\n", SPCInfo.SongTitle);
printf(" Game: %s\n", SPCInfo.GameTitle);
printf(" Dumper: %s\n", SPCInfo.DumperName);
printf(" Comments: %s\n", SPCInfo.Comment);
printf(" Created on: %s\n", SPCInfo.Date);

printf("\n");

fflush(stdout);
}

SNDNoteOn(SPC_DSP[0x4c]);

int success = 1;
int seconds = SNDratecnt = 0;
while (true)
{
if (ITMix() || ITUpdate())
return 1;

SNDratecnt += 1;

int ret = SPC_START(2048000 / (SPCUpdateRate * 2)); // emulate the SPC700

if (ret)
return 1;

if (SNDratecnt >= SPCUpdateRate)
{
SNDratecnt -= SPCUpdateRate;
seconds++; // count number of seconds

if (verbose)
{
printf("Progress: %f%%\r", (((f64)seconds / limit) * 100));
fflush(stdout);
}

if (seconds == limit)
break;
}
}

return 0;
}

int spc2it_save(const char* dest, int verbose)
{
if (verbose)
printf("\n\nSaving file...\n");

if (ITWrite(dest))
{
if (verbose)
printf("Error: failed to write %s.\n", dest);

return 1;
}

if (verbose)
printf("Wrote to %s successfully.\n", dest);

return 0;
}

void spc2it_cleanup()
{
ITCleanup();
Reset_SPC();
}

int spc2it_convert(const char* src, const char* dest)
{
int failure = spc2it_load(src, 200, 0, 0) || spc2it_save(dest, 0);

spc2it_cleanup();

return failure;
}
12 changes: 12 additions & 0 deletions src/lib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/****************************************************
*Part of SPC2IT, read readme.md for more information*
****************************************************/

#ifndef LIB_H
#define LIB_H

int spc2it_load(const char* src, int ITrows, int limit, int verbose);
int spc2it_save(const char* dest, int verbose);
void spc2it_cleanup();

#endif
Loading

0 comments on commit b70316e

Please sign in to comment.