-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdload.cpp
More file actions
129 lines (96 loc) · 2.86 KB
/
sdload.cpp
File metadata and controls
129 lines (96 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//
// sdload.cpp
//
// To do: more patches
// To do: saving patches
//
#include <circle/string.h>
#include <circle/util.h>
#include "sdload.h"
float CSDLoad::s_Buffer[MAX_TABLES * TABLE_SIZE] = { 0.0f };
unsigned CSDLoad::s_TableSize = TABLE_SIZE;
unsigned CSDLoad::s_NumTables = 0;
CSDLoad::CSDLoad(CInterruptSystem* pInterrupt, CTimer* pTimer)
: m_EMMC(pInterrupt, pTimer),
m_patchCount(0)
{
memset(m_patchBuffer, 0, sizeof(m_patchBuffer));
memset(m_patchBufferSize, 0, sizeof(m_patchBufferSize));
}
float* CSDLoad::GetBuffer() { return s_Buffer; }
unsigned CSDLoad::GetNumTables() { return s_NumTables; }
unsigned CSDLoad::GetTableSize() { return s_TableSize; }
bool CSDLoad::Initialize()
{
if (!m_EMMC.Initialize())
return false;
return true;
}
bool CSDLoad::LoadWavetable() // LOAD WAVETABLES
{
if (f_mount(&m_FileSystem, DRIVE, 1) != FR_OK)
{ return false; }
FIL File;
FRESULT Result = f_open(&File, DRIVE FILENAME, FA_READ | FA_OPEN_EXISTING);
if (Result != FR_OK)
{
f_mount(nullptr, DRIVE, 0);
return false;
}
// Optional 8-byte header (ignored)
uint32_t header[2] = {0, 0};
UINT bytesRead = 0;
f_read(&File, header, sizeof(header), &bytesRead);
s_NumTables = header[0];
s_TableSize = header[1];
unsigned totalTables = s_NumTables * 2;
unsigned totalSamples = totalTables * s_TableSize;
if (totalTables > MAX_TABLES)
totalTables = MAX_TABLES;
if (totalSamples > MAX_TABLES * TABLE_SIZE)
totalSamples = MAX_TABLES * TABLE_SIZE;
memset(s_Buffer, 0, sizeof(s_Buffer));
Result = f_read(&File, s_Buffer, totalSamples * sizeof(float), &bytesRead);
f_close(&File);
f_mount(nullptr, DRIVE, 0);
if (Result != FR_OK || bytesRead != totalSamples * sizeof(float))
{
memset(s_Buffer, 0, sizeof(s_Buffer));
return false;
}
return true;
}
bool CSDLoad::LoadPatches() // LOAD PATCHES
{
m_patchCount = 0;
// Fixed array of filenames
const char* patchFiles[MAX_PATCHES] =
{
"SD:/patches/patch0.txt",
"SD:/patches/patch1.txt",
"SD:/patches/patch2.txt",
"SD:/patches/patch3.txt",
"SD:/patches/patch4.txt",
"SD:/patches/patch5.txt",
"SD:/patches/patch6.txt",
"SD:/patches/patch7.txt"
};
if (f_mount(&m_FileSystem, DRIVE, 1) != FR_OK)
{ return false; }
for (unsigned i = 0; i < MAX_PATCHES; ++i)
{
FIL File;
if(f_open(&File, patchFiles[i], FA_READ | FA_OPEN_EXISTING) != FR_OK)
break; // stop if file not found
UINT bytesRead = 0;
if (f_read(&File, m_patchBuffer[i], sizeof(m_patchBuffer[i])-1, &bytesRead) != FR_OK)
return false;
if (!bytesRead)
return false;
f_close(&File);
m_patchBuffer[i][bytesRead] = '\0'; // Ensure null terminated
m_patchBufferSize[i] = bytesRead;
++m_patchCount;
}
return (m_patchCount > 0);
}