Skip to content

Commit

Permalink
Extract util in to library
Browse files Browse the repository at this point in the history
  • Loading branch information
Yury Klushin committed Nov 6, 2024
1 parent 4b12796 commit d183bad
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 37 deletions.
5 changes: 2 additions & 3 deletions emulator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_subdirectory(emubase)
add_subdirectory(util)

find_package(Qt5 REQUIRED COMPONENTS Widgets LinguistTools Script Multimedia Test)

Expand All @@ -16,7 +17,6 @@ set(CMAKE_AUTORCC ON)
set(PROJECT_SOURCES
main.cpp
mainwindow.cpp
util/WavPcmFile.cpp
Common.cpp
Emulator.cpp
qscreen.cpp
Expand All @@ -34,7 +34,6 @@ set(PROJECT_SOURCES
set(PROJECT_HEADERS
mainwindow.h
stdafx.h
util/WavPcmFile.h
Common.h
Emulator.h
qscreen.h
Expand Down Expand Up @@ -65,7 +64,7 @@ add_executable(QtUkncBtl
)

target_include_directories(QtUkncBtl PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(QtUkncBtl PRIVATE emubase Qt5::Widgets Qt5::Script Qt5::Multimedia Qt5::Test)
target_link_libraries(QtUkncBtl PRIVATE emubase util Qt5::Widgets Qt5::Script Qt5::Multimedia Qt5::Test)

set_target_properties(QtUkncBtl PROPERTIES
${BUNDLE_ID_OPTION}
Expand Down
22 changes: 22 additions & 0 deletions emulator/util/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
cmake_minimum_required(VERSION 3.16)

project(util LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(UTIL_SOURCES
WavPcmFile.cpp
)

set(UTIL_HEADERS
stdafx.h
WavPcmFile.h
)

add_library(util STATIC
${UTIL_SOURCES}
${UTIL_HEADERS}
)

target_compile_definitions(util PRIVATE EMUBASE_LIBRARY)
62 changes: 31 additions & 31 deletions emulator/util/WavPcmFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ struct WAVPCMFILE
int nBitsPerSample;
int nSampleFrequency;
int nBlockAlign;
quint32 dwDataOffset;
quint32 dwDataSize;
quint32 dwCurrentPosition;
uint32_t dwDataOffset;
uint32_t dwDataSize;
uint32_t dwCurrentPosition;
bool okWriting;
};

Expand All @@ -49,7 +49,7 @@ int WavPcmFile_GetFrequency(HWAVPCMFILE wavpcmfile)
return pWavPcm->nSampleFrequency;
}

quint32 WavPcmFile_GetLength(HWAVPCMFILE wavpcmfile)
uint32_t WavPcmFile_GetLength(HWAVPCMFILE wavpcmfile)
{
if (wavpcmfile == INVALID_HANDLE_VALUE)
return 0;
Expand All @@ -59,7 +59,7 @@ quint32 WavPcmFile_GetLength(HWAVPCMFILE wavpcmfile)
return pWavPcm->dwDataSize / pWavPcm->nBlockAlign;
}

quint32 WavPcmFile_GetPosition(HWAVPCMFILE wavpcmfile)
uint32_t WavPcmFile_GetPosition(HWAVPCMFILE wavpcmfile)
{
if (wavpcmfile == INVALID_HANDLE_VALUE)
return 0;
Expand All @@ -69,14 +69,14 @@ quint32 WavPcmFile_GetPosition(HWAVPCMFILE wavpcmfile)
return pWavPcm->dwCurrentPosition;
}

void WavPcmFile_SetPosition(HWAVPCMFILE wavpcmfile, quint32 position)
void WavPcmFile_SetPosition(HWAVPCMFILE wavpcmfile, uint32_t position)
{
if (wavpcmfile == INVALID_HANDLE_VALUE)
return;

WAVPCMFILE* pWavPcm = (WAVPCMFILE*) wavpcmfile;

quint32 offsetInData = position * pWavPcm->nBlockAlign;
uint32_t offsetInData = position * pWavPcm->nBlockAlign;
::fseek(pWavPcm->fpFile, pWavPcm->dwDataOffset + offsetInData, SEEK_SET);

pWavPcm->dwCurrentPosition = position;
Expand All @@ -93,21 +93,21 @@ HWAVPCMFILE WavPcmFile_Create(LPCTSTR filename, int sampleRate)
return (HWAVPCMFILE) INVALID_HANDLE_VALUE; // Failed to create file

// Prepare and write file header
quint8 consolidated_header[12 + 8 + 16 + 8];
uint8_t consolidated_header[12 + 8 + 16 + 8];
::memset(consolidated_header, 0, sizeof(consolidated_header));
size_t bytesWritten;

memcpy(&consolidated_header[0], magic1, 4); // RIFF
memcpy(&consolidated_header[8], magic2, 4); // WAVE

memcpy(&consolidated_header[12], format_tag_id, 4); // fmt
*((quint32*)(consolidated_header + 16)) = 16; // Size of "fmt" chunk
*((quint16*)(consolidated_header + 20)) = WAV_FORMAT_PCM; // AudioFormat = PCM
*((quint16*)(consolidated_header + 22)) = channels; // NumChannels = mono
*((quint32*)(consolidated_header + 24)) = sampleRate; // SampleRate
*((quint32*)(consolidated_header + 28)) = sampleRate * channels * bitsPerSample / 8; // ByteRate
*((quint16*)(consolidated_header + 32)) = blockAlign;
*((quint16*)(consolidated_header + 34)) = bitsPerSample;
*((uint32_t*)(consolidated_header + 16)) = 16; // Size of "fmt" chunk
*((uint16_t*)(consolidated_header + 20)) = WAV_FORMAT_PCM; // AudioFormat = PCM
*((uint16_t*)(consolidated_header + 22)) = channels; // NumChannels = mono
*((uint32_t*)(consolidated_header + 24)) = sampleRate; // SampleRate
*((uint32_t*)(consolidated_header + 28)) = sampleRate * channels * bitsPerSample / 8; // ByteRate
*((uint16_t*)(consolidated_header + 32)) = blockAlign;
*((uint16_t*)(consolidated_header + 34)) = bitsPerSample;

memcpy(&consolidated_header[36], data_tag_id, 4); // data

Expand Down Expand Up @@ -146,13 +146,13 @@ HWAVPCMFILE WavPcmFile_Open(LPCTSTR filename)
if (fpFileOpen == nullptr)
return (HWAVPCMFILE) INVALID_HANDLE_VALUE; // Failed to open file

quint32 offset = 0;
uint32_t offset = 0;
size_t bytesRead;
::fseek(fpFileOpen, 0, SEEK_END);
quint32 fileSize = ::ftell(fpFileOpen);
long fileSize = ::ftell(fpFileOpen);
::fseek(fpFileOpen, 0, SEEK_SET);

quint8 fileHeader[12];
uint8_t fileHeader[12];
bytesRead = ::fread(fileHeader, 1, sizeof(fileHeader), fpFileOpen);
if (bytesRead != sizeof(fileHeader) ||
memcmp(&fileHeader[0], magic1, 4) != 0 ||
Expand All @@ -161,17 +161,17 @@ HWAVPCMFILE WavPcmFile_Open(LPCTSTR filename)
::fclose(fpFileOpen);
return (HWAVPCMFILE) INVALID_HANDLE_VALUE; // Failed to read file header OR invalid 'RIFF' tag OR invalid 'WAVE' tag
}
offset += (quint32)bytesRead;
offset += (uint32_t)bytesRead;

quint32 statedSize = *((quint32*)(fileHeader + 4)) + 8;
long statedSize = *((uint32_t*)(fileHeader + 4)) + 8;
if (statedSize > fileSize)
statedSize = fileSize;

quint8 tagHeader[8];
quint16 formatTag[8];
uint8_t tagHeader[8];
uint16_t formatTag[8];
bool formatSpecified = false;
int formatType, channels = 1, bitsPerSample, blockAlign;
quint32 sampleFrequency, bytesPerSecond, dataOffset, dataSize = 0;
uint32_t sampleFrequency, bytesPerSecond, dataOffset, dataSize = 0;
while (offset < statedSize)
{
bytesRead = ::fread(tagHeader, 1, sizeof(tagHeader), fpFileOpen);
Expand All @@ -180,9 +180,9 @@ HWAVPCMFILE WavPcmFile_Open(LPCTSTR filename)
::fclose(fpFileOpen);
return (HWAVPCMFILE) INVALID_HANDLE_VALUE; // Failed to read tag header
}
offset += (quint32)bytesRead;
offset += (uint32_t)bytesRead;

quint32 tagSize = *(quint32*)(tagHeader + 4);
uint32_t tagSize = *(uint32_t*)(tagHeader + 4);
if (!memcmp(tagHeader, format_tag_id, 4))
{
if (formatSpecified || tagSize < sizeof(formatTag))
Expand Down Expand Up @@ -270,7 +270,7 @@ void WavPcmFile_Close(HWAVPCMFILE wavpcmfile)
size_t bytesWritten;
// Write data chunk size
::fseek(pWavPcm->fpFile, 4, SEEK_SET);
quint32 chunkSize = 36 + pWavPcm->dwDataSize;
uint32_t chunkSize = 36 + pWavPcm->dwDataSize;
bytesWritten = ::fwrite(&chunkSize, 1, 4, pWavPcm->fpFile);
// Write data subchunk size
::fseek(pWavPcm->fpFile, 40, SEEK_SET);
Expand All @@ -293,7 +293,7 @@ bool WavPcmFile_WriteOne(HWAVPCMFILE wavpcmfile, unsigned int value)
ASSERT(pWavPcm->nBitsPerSample == 8);
ASSERT(pWavPcm->nChannels == 1);

quint8 data = (value >> 24) & 0xff;
uint8_t data = (value >> 24) & 0xff;

size_t bytesWritten = ::fwrite(&data, 1, 1, pWavPcm->fpFile);
if (bytesWritten != 1)
Expand All @@ -315,9 +315,9 @@ unsigned int WavPcmFile_ReadOne(HWAVPCMFILE wavpcmfile)
return 0;

// Read one sample
quint32 bytesToRead = pWavPcm->nBlockAlign;
uint32_t bytesToRead = pWavPcm->nBlockAlign;
size_t bytesRead;
quint8 data[16];
uint8_t data[16];
bytesRead = ::fread(data, 1, bytesToRead, pWavPcm->fpFile);
if (bytesRead != bytesToRead)
return 0;
Expand All @@ -333,11 +333,11 @@ unsigned int WavPcmFile_ReadOne(HWAVPCMFILE wavpcmfile)
value = value << 24;
break;
case 16:
value = *((quint16*)data);
value = *((uint16_t*)data);
value = value << 16;
break;
case 32:
value = *((quint32*)data);
value = *((uint32_t*)data);
break;
}

Expand Down
8 changes: 5 additions & 3 deletions emulator/util/WavPcmFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ UKNCBTL. If not, see <http://www.gnu.org/licenses/>. */

#pragma once

#include <cstdint>

//////////////////////////////////////////////////////////////////////

DECLARE_HANDLE(HWAVPCMFILE);
Expand All @@ -26,12 +28,12 @@ void WavPcmFile_Close(HWAVPCMFILE wavpcmfile);
// Samples per second, Hz
int WavPcmFile_GetFrequency(HWAVPCMFILE wavpcmfile);
// Length of the stream, in samples
quint32 WavPcmFile_GetLength(HWAVPCMFILE wavpcmfile);
uint32_t WavPcmFile_GetLength(HWAVPCMFILE wavpcmfile);

// Current position in the stream, in samples, zero-based
quint32 WavPcmFile_GetPosition(HWAVPCMFILE wavpcmfile);
uint32_t WavPcmFile_GetPosition(HWAVPCMFILE wavpcmfile);
// Set current position in the stream, in samples, zero-based
void WavPcmFile_SetPosition(HWAVPCMFILE wavpcmfile, quint32 position);
void WavPcmFile_SetPosition(HWAVPCMFILE wavpcmfile, uint32_t position);

// Read one sample scaled to int type range
unsigned int WavPcmFile_ReadOne(HWAVPCMFILE wavpcmfile);
Expand Down
71 changes: 71 additions & 0 deletions emulator/util/stdafx.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once

#include <cstdint>
#include <cassert>
#include <cstdlib>
#include <cstdio>
#include <cstring>

#ifndef CALLBACK
# define CALLBACK
#endif // CALLBACK

#ifndef TCHAR
typedef char TCHAR;
#define _tfopen fopen
#define _tcscpy strcpy
#define _tstat _stat
#define _tcsrchr strrchr
#define _tcsicmp strcasecmp
#define _tcscmp strcmp
#define _tcslen strlen
#define _sntprintf _snprintf
#define _snprintf snprintf
#endif // TCHAR

#ifndef _T
# define _T(str) (str)
#endif // _T

#ifndef LPTSTR
typedef TCHAR* LPTSTR;
#endif // LCTSTR

#ifndef LPCTSTR
typedef const TCHAR* LPCTSTR;
#endif // LPCTSTR

#ifndef ASSERT
# define ASSERT(expr) assert(expr)
#endif // ASSERT

#ifndef MAKEWORD
# define MAKEWORD(a, b) ((uint16_t)(((uint8_t)(((uint32_t)(a)) & 0xff)) | ((uint16_t)((uint8_t)(((uint32_t)(b)) & 0xff))) << 8))
#endif // MAKEWORD

#ifndef HANDLE
typedef void *HANDLE;
#endif // HANDLE

#ifndef DECLARE_HANDLE
# define DECLARE_HANDLE(name) struct name##__ { int unused; }; typedef struct name##__ *name
#endif // DECLARE_HANDLE

#ifndef INVALID_HANDLE_VALUE
# define INVALID_HANDLE_VALUE ((HANDLE)(int32_t)-1)
#endif // INVALID_HANDLE_VALUE

//////////////////////////////////////////////////////////////////////
// DebugPrint

void DebugPrint(const char* message);
void DebugPrintFormat(const char* pszFormat, ...);
void DebugLog(const char* message);
void DebugLogFormat(const char* pszFormat, ...);

void PrintOctalValue(char* buffer, uint16_t value);

0 comments on commit d183bad

Please sign in to comment.