PageRenderTime 2ms CodeModel.GetById 61ms app.highlight 12ms RepoModel.GetById 58ms app.codeStats 0ms

/src/js/libs/Sorollet-legacy.js

https://github.com/ehsan/to_the_beat
JavaScript | 199 lines | 104 code | 43 blank | 52 comment | 6 complexity | 3a5e43e3d3124ee06b97c92b30486967 MD5 | raw file
  1// Something to be able to use old (~2008) data with recent Sorollet versions
  2SOROLLET.Legacy = {};
  3SOROLLET.Legacy.loadSongFromArray = function(player, song) {
  4	var i = 0, j,
  5		size = song.length,
  6		voice,
  7		M_PI_2 = Math.PI / 2.0;
  8
  9	player.voices = [];
 10	player.patterns = [];
 11	player.orderList = [];
 12
 13	while(i < size) { // -1?
 14
 15		// bpm, speed, num channels
 16		player.setBPM(song[i]); i++;
 17		// legacy // player.setSpeed(song[i]);
 18		i++;
 19		var numVoices = song[i]; i++;
 20
 21		// legacy
 22		//sorollet_song.frames_per_second = sorollet_song.bpm * 0.4f;
 23		//sorollet_song.seconds_per_row = sorollet_song.speed / sorollet_song.frames_per_second;
 24
 25		// Init voices
 26		for(j = 0; j < numVoices; j++) {
 27			voice = new SOROLLET.Voice();
 28			player.voices.push( voice );
 29		}
 30
 31		// Order list
 32		var numOrders = song[i]; i++;
 33
 34		for(j = 0; j < numOrders; j++) {
 35			player.orderList.push(song[i]); i++;
 36		}
 37
 38		// Patterns
 39		var numPatterns = song[i]; i++;
 40
 41		for(j = 0; j < numPatterns; j++) {
 42			var numRows = song[i]; i++;
 43			var pattern = new SOROLLET.Pattern(numVoices, numRows);
 44
 45			for(var row = 0; row < numRows; row++) {
 46
 47				for(var col = 0; col < numVoices; col++) {
 48
 49					var cell = pattern.getCell(row, col);
 50					cell.note = song[i]; i++;
 51					cell.volume = charToFloat(song[i], 0.0, 1.0); i++;
 52
 53				}
 54
 55			}
 56
 57			player.patterns.push(pattern);
 58		}
 59
 60		// Synths config
 61		for(j = 0; j < numVoices; j++) {
 62			
 63			voice = player.voices[j];
 64
 65			var oscillatorMix = charToFloat(song[i], 0.0, 1.0); i++; // TODO to wave1Volume, wave2Volume
 66			voice.currentVolume = charToFloat(song[i], 0.0, 1.0); i++;
 67
 68			// Osc1
 69			voice.wave1Function = enumToWave(song[i]); i++;
 70			voice.wave1Phase = charToFloat(song[i], -M_PI_2, M_PI_2); i++;
 71			voice.wave1Octave = song[i]; i++;
 72
 73			// Osc2
 74			voice.wave2Function = enumToWave(song[i]); i++;
 75			voice.wave2Phase = charToFloat(song[i], -M_PI_2, M_PI_2); i++;
 76			voice.wave2Octave = song[i]; i++;
 77
 78			// Noise level
 79			voice.noiseAmount = charToFloat(song[i], 0.0, 1.0); i++;
 80
 81			
 82			// Begins section of "massively not implemented features"
 83			// Filter
 84			// type
 85			// sorollet_synths[j].filter_type = song[i]; i++;
 86			i++;
 87
 88			// frequency
 89			// sorollet_synths[j].filter_frequency = charToFloat(song[i], 22.0f, 300.0);
 90			i++;
 91			// resonance
 92			// sorollet_synths[j].filter_resonance = charToFloat(song[i], 0.0, 0.2f);
 93			i++;
 94
 95			// Saturate
 96			// sorollet_synths[j].saturate_active = song[i];
 97			i++;
 98			//sorollet_synths[j].saturate_max = charToFloat(song[i], 0.0, 1.0);
 99			i++;
100
101			// EQ
102			// sorollet_synths[j].eq_active = song[i];
103			i++;
104			// sorollet_synths[j].eq_state.lg = charToFloat(song[i], 0.0, 10.0);
105			i++;
106			// sorollet_synths[j].eq_state.mg = charToFloat(song[i], 0.0, 10.0);
107			i++;
108			// sorollet_synths[j].eq_state.hg = charToFloat(song[i], 0.0, 10.0);
109			i++;
110
111			// Bass boost
112			// sorollet_synths[j].bass_boost_active = song[i];
113			i++;
114			// sorollet_synths[j].bass_boost_multiplier = charToFloat(song[i], 0.0, 8.0f);
115			i++;
116
117
118			// Envelopes
119			var attack_time;
120			var decay_time;
121			var sustain_level;
122			var release_time;
123			var env;
124
125			// Amp envelope
126			// sorollet_synths[j].amp_envelope_active = song[i];
127			i++;
128			attack_time = charToFloat(song[i], 0.0, 16.0); i++;
129			decay_time = charToFloat(song[i], 0.0, 16.0); i++;
130			sustain_level = charToFloat(song[i], 0.0, 1.0); i++;
131			release_time = charToFloat(song[i], 0.0, 16.0); i++;
132
133			// sorollet_adsr_set_values(&sorollet_synths[j].amp_envelope, attack_time, decay_time, sustain_level, release_time);
134			env = voice.volumeEnvelope;
135			env.setAttack(attack_time);
136			env.setDecay(decay_time);
137			env.setSustainLevel(sustain_level);
138			env.setRelease(release_time);
139
140			// Pitch envelope
141			// sorollet_synths[j].pitch_envelope_active = song[i];
142			i++;
143			
144			attack_time = charToFloat(song[i], 0.0, 16.0); i++;
145			decay_time = charToFloat(song[i], 0.0, 16.0); i++;
146			sustain_level = charToFloat(song[i], 0.0, 1.0); i++;
147			release_time = charToFloat(song[i], 0.0, 16.0); i++;
148
149			//sorollet_adsr_set_values(&sorollet_synths[j].pitch_envelope, attack_time, decay_time, sustain_level, release_time);
150			env = voice.pitchEnvelope;
151			env.setAttack(attack_time);
152			env.setDecay(decay_time);
153			env.setSustainLevel(sustain_level);
154			env.setRelease(release_time);
155
156
157			// Filter frequency envelope
158			// TODO not implemented
159			// sorollet_synths[j].filter_freq_envelope_active = song[i];
160			i++;
161			// attack_time = charToFloat(song[i], 0.0, 16.0);
162			i++;
163			// decay_time = charToFloat(song[i], 0.0, 16.0);
164			i++;
165			// sustain_level = charToFloat(song[i], 0.0, 1.0);
166			i++;
167			// release_time = charToFloat(song[i], 0.0, 16.0);
168			i++;
169
170			//sorollet_adsr_set_values(&sorollet_synths[j].filter_freq_envelope, attack_time, decay_time, sustain_level, release_time);
171
172			// Precalc filter parameters if the envelope is not used
173			/*if(!sorollet_synths[j].filter_freq_envelope_active)
174			{
175				sorollet_synth_prepare_filter(&sorollet_synths[j], sorollet_synths[j].filter_frequency);
176			}*/
177
178		}
179
180		return 0;
181	}
182	return -1;
183
184	function charToFloat(value, outMin, outMax) {
185		var tmp_value = value / 255.0;
186		var out = tmp_value * (outMax - outMin) + outMin;
187		return out;
188	}
189
190	function enumToWave(value) {
191		switch(value) {
192			case 1: return SOROLLET.Voice.prototype.getTriangleBuffer;
193			case 2: return SOROLLET.Voice.prototype.getSquareBuffer;
194			case 3: return SOROLLET.Voice.prototype.getSawtoothBuffer;
195		}
196		return SOROLLET.Voice.prototype.getSineBuffer;
197	}
198	
199};