summaryrefslogtreecommitdiff
path: root/synth/main.cpp
blob: 32b2b8407e12e5d7e74da449a86ab342c0dd7dc4 (plain)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <string>
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <pthread.h>

#include "jack.h"
#include "load.h"
#include "cli.h"
#include "channel.h"
#include "fixed.h"
#include "programs.h"
#include "defines.h"
#include "globals.h"
#include "in_synth_cli.h"
#include "communication.h"
#include "note_loader.h"
#include "lfos.h"
#include "watch_files.h"

using namespace std;


void cleanup();
void dump_options();

pthread_t watcher_thread=-1;

int main(int argc, char** argv)
{	
	init_communication();
	
  for (int i=0;i<N_LFOS;++i)
  	lfo_freq_hz[i]=0;
  
  try
  {
		parse_args(argc, argv);
		
		add_dir("~/.flosynth", false);
		add_dir("/etc/flosynth", false);

		if (cleanup_interval_sec<=0) cleanup_interval_sec=CLEANUP_INTERVAL_SEC;
		for (int i=0;i<N_LFOS;++i)
			if (lfo_freq_hz[i]<=0) lfo_freq_hz[i]=LFO_FREQ_HZ[i];

		if (snh_freq_hz<=0) snh_freq_hz=SNH_FREQ_HZ;
		if (frameskip<=-1) frameskip=0;
		if (max_port_time_sec<=0) max_port_time_sec=MAX_PORTAMENTO_TIME;
		if (filter_update_freq_hz<=0) filter_update_freq_hz=FILTER_UPDATE_FREQ_HZ;
		if (lfo_update_freq_hz<=0) lfo_update_freq_hz=LFO_UPDATE_FREQ_HZ;
		if (envelope_update_freq_hz<=0) envelope_update_freq_hz=ENVELOPE_UPDATE_FREQ_HZ;
		if (xrun_n<=0) xrun_n=XRUN_N;
		if (xrun_time<=0) xrun_time=XRUN_TIME;
		
		dump_options();
		
		frameskip+=1; //because a value of 0 means using each frame,
		              //a value of 1 means using each 2nd frame and so on
		
		init_jack();

		//this calculation needs the real sampling rate. others don't
		cleanup_interval=cleanup_interval_sec*samp_rate;
		
		
		#ifdef FRAMESKIP			
			samp_rate/=frameskip;
		#endif


		filter_update_frames=samp_rate/filter_update_freq_hz;
		lfo_update_frames=samp_rate/lfo_update_freq_hz;
		envelope_update_frames=samp_rate/envelope_update_freq_hz;
		if (filter_update_frames<1) filter_update_frames=1;
		if (lfo_update_frames<1) lfo_update_frames=1;
		if (envelope_update_frames<1) envelope_update_frames=1;

		int i;


		init_default_program(default_program);


		init_snh();
		
		for (i=0;i<N_LFOS;++i)
			init_lfo(i);
		
		program_settings=new program_t[128];

		for (i=0;i<128;++i)
		{
			program_lock[i]=false;
			
			load_program(programfile[i],program_settings[i]);
		}
		
		for (i=0;i<N_WAVEFORMS;++i)
			wave[i]=new fixed_t[WAVE_RES];
			
		for (i=0;i<WAVE_RES;++i)
		{
			float temp1, temp2;
			temp1=sin(i*2.0*3.141592654/WAVE_RES);
			temp2=sin(i*3.141592654/WAVE_RES);
			wave[0][i]=temp1*ONE;
			wave[1][i]=temp2*ONE;
			wave[2][i]=(i<=WAVE_RES/2) ? wave[0][i] : 0;
			wave[3][i]= (i<=WAVE_RES/2) ? wave[1][i] : 0;
			wave[4][i]=(i<WAVE_RES/2) ? ONE : -ONE;
			wave[5][i]=(i<=WAVE_RES/2) ? (ONE*2*i/WAVE_RES) : (ONE*2*i/WAVE_RES - 2*ONE);
			wave[6][i]=(i<=WAVE_RES/2) ? (ONE*2*i/WAVE_RES) : (ONE*2*(WAVE_RES-i)/WAVE_RES);
			wave[7][i]=float(rand() - RAND_MAX/2) / (RAND_MAX/2) *ONE;
		}
		
		for (int i=0;i<N_CHANNELS;++i)
			channel[i]=new Channel;
		
		srand (time(NULL));
		
		start_jack(connect_audio, connect_midi);
		
		if (watchfiles)
		{
			if (pthread_create(&watcher_thread, NULL, watch_files, NULL) != 0)
			{
				output_warning("WARNING: could not start file-watcher thread. you must inform me about\n"
				               "         updated files manually.");
				watcher_thread=-1;
			}
		}
		else
		{
			output_note("NOTE: you disabled the watching of files. you must inform me about\n"
									"         updated files manually.");
		}
			
		do_in_synth_cli();
		
		cleanup();
	}
	catch(string err)
	{
		cout << endl<<endl<< "FATAL: caught an exception: "<<endl<<err<<endl<<"exiting..." << endl;
	}
/*	catch(...)
	{
		cout << "FATAL: caught an unknown exception... exiting..." << endl;
	}*/
	return 0;
}

void cleanup()
{
	if (watcher_thread!=-1)
	{
		if (pthread_cancel(watcher_thread) != 0)
		{
			output_warning("WARNING: could not cancel watcher thread!");
		}
		else
		{
			pthread_join(watcher_thread,NULL);
		}
	}
	
	exit_jack();
	
	uninit_communication();
	
	for (int i=0;i<N_CHANNELS;++i)
	{
		delete channel[i];
		channel[i]=NULL;
	}
	
	for (int i=0;i<128;++i)
		maybe_unload_note(program_settings[i]);
	
	delete [] program_settings;
}

void dump_options()
{
	for (int i=0;i<128;++i)
		if (programfile[i]!="")
			cout << "program #"<<i<<":\t'"<<programfile[i]<<"'"<<endl;
	
	cout << endl;
	
	cout << "frameskip:\t\t"<<frameskip<<endl;
	cout << "cleanup-interval:\t"<<cleanup_interval_sec<<endl;
	for (int i=0;i<N_LFOS;++i)
		cout << "lfo"<<i<<" freq:\t\t"<<lfo_freq_hz[i]<<endl;

	cout << "sample and hold freq:\t"<<snh_freq_hz<<endl;
	cout << "max portamento time:\t"<<max_port_time_sec<<endl;
	cout << "xrun n/time:\t\t"<<xrun_n<<"/"<<xrun_time<<"s"<<endl;
	cout << "lfo update freq:\t"<<lfo_update_freq_hz<<endl;
	cout << "filter update freq:\t"<<filter_update_freq_hz<<endl;
	cout << "envelope update freq:\t"<<envelope_update_freq_hz<<endl;
	
}