-
Notifications
You must be signed in to change notification settings - Fork 2
/
program.cpp
166 lines (137 loc) · 4.28 KB
/
program.cpp
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Program.cpp: implementation of the ProgramChanger class.
//
//////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <string.h>
#include "program.h"
#include "settings.h"
#define programsfile "programs.ini"
/////////////////////////////////
// Program
Program::Program()
{
int i;
for (i=0; i<N_LOOPS; i++)
m_strFile[i][0] = 0;
}
/////////////////////////////////
// ProgramChanger
ProgramChanger::ProgramChanger()
{
m_Program = NULL;
m_hThread = 0;
m_nLoadingQueueW = m_nLoadingQueueR = 0;
}
ProgramChanger::~ProgramChanger()
{
if (m_Program)
delete[] m_Program;
}
/*!
** Load programs from programs.ini
*/
void ProgramChanger::LoadPrograms()
{
char line[1024], param[256], value[MAX_PATH];
int len, pos, mark, n;
int number=1, loop=0;
// count number of programs
SettingsFile f(programsfile);
if (!f.IsOpenForRead()) {
printf("No programs found.\n");
return;
}
m_nPrograms = 0;
while (f.ReadSetting())
if (f.m_bSectionChanged) m_nPrograms++;
f.Close();
printf("%d program%s found.\n", m_nPrograms, m_nPrograms==1 ? "" : "s");
// allocate
m_Program = new Program[m_nPrograms];
// rewind
if (!f.OpenForRead(programsfile)) {
printf("Error re-opening %s\n", programsfile);
return;
}
n=-1;
while (f.ReadSetting())
{
if (f.m_bSectionChanged) {
n++;
m_Program[n].m_nProgramNumber = n+1;
strncpy(m_Program[n].m_strName, f.m_strSection, Program::m_nNameSize);
loop = 1;
}
if (strcasecmp(f.m_strParam, "Dir")==0 && n>=0 && n<m_nPrograms) {
strncpy(m_Program[n].m_strDir, f.m_strValue, Program::m_nDirSize);
}
else if (strcasecmp(f.m_strParam, "Loop")==0 && n>=0 && n<m_nPrograms) {
pos = loop++;
if (pos >= 1 && pos <= N_LOOPS)
strncpy(m_Program[n].m_strFile[pos-1], f.m_strValue, Program::m_nFileNameSize);
}
}
printf("%d program%s loaded.\n", m_nPrograms, m_nPrograms==1 ? "" : "s");
}
/*!
* Load the samples in the queue in order, and drop them into their corresponding LoopOb instances as
* "background samples" once they are loaded.
*/
THREADFUNC ProgramChanger::loadBackgroundSamplesThread(void* param)
{
#ifndef WIN32
pthread_detach(pthread_self());
#endif
LOWPRIORITY();
ProgramChanger &pc = *((ProgramChanger*)param);
while (pc.m_nLoadingQueueR != pc.m_nLoadingQueueW) {
char *strFilename = (char*)pc.m_pLoadingQueue[pc.m_nLoadingQueueR++];
LoopOb *pLoopOb = (LoopOb*)pc.m_pLoadingQueue[pc.m_nLoadingQueueR++];
pc.m_nLoadingQueueR = pc.m_nLoadingQueueR % 20;
// Skip it if it has the same filename as foreground sample or previous background sample
if (pLoopOb->GetSample() && strcmp(pLoopOb->GetSample()->m_filename, strFilename)==0)
continue;
if (pLoopOb->GetBackgroundSample() && strcmp(pLoopOb->GetBackgroundSample()->m_filename, strFilename)==0)
continue;
Sample *pSample = new Sample();
if (pSample->LoadFromFile(strFilename)) {
pLoopOb->SetBackgroundSample(pSample);
}
else
printf("Error loading %s as background sample.\n", strFilename);
}
pc.m_hThread = 0;
}
/*!
** Setup m_pLoopOb array to switch to new program on zero volume.
*/
void ProgramChanger::ProgramChange(int program, LoopOb* m_pLoopOb[N_LOOPS])
{
// Find the program
int i;
for (i=0; i<m_nPrograms; i++)
if (m_Program[i].m_nProgramNumber == program) break;
if (i>=m_nPrograms) {
printf("Program %d not found.\n", program);
return;
}
// If we are already loading something, clear the queue.
m_nLoadingQueueR = m_nLoadingQueueW;
// Put each sample filename onto the queue along with its
// associated LoopOb object.
int loop;
for (loop=0; loop<N_LOOPS; loop++)
{
if (m_Program[i].m_strFile[loop][0]) {
m_pLoadingQueue[m_nLoadingQueueW] = m_Program[i].m_strFile[loop];
m_pLoadingQueue[m_nLoadingQueueW+1] = m_pLoopOb[loop];
m_nLoadingQueueW = (m_nLoadingQueueW+2) % 20;
}
if (m_Program[i].m_strDir[0]) {
m_pLoopOb[loop]->m_pFileBrowser->SetDirectoryFromBase(m_Program[i].m_strDir);
}
}
printf("Changing to program %d.\n", program);
if (!m_hThread)
CREATETHREAD(m_hThread, loadBackgroundSamplesThread, (void*)this);
}