Permalink
Browse files

added simple waveform generator

  • Loading branch information...
1 parent 8f61ecb commit ba18d31c886c79c614039557ba706c7854a9a1fc @scribeGriff committed Sep 4, 2012
View

Large diffs are not rendered by default.

Oops, something went wrong.
View

Large diffs are not rendered by default.

Oops, something went wrong.
View

Large diffs are not rendered by default.

Oops, something went wrong.
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -19,6 +19,7 @@
#source('utilities/rsel_results.dart');
#source('utilities/fft_results.dart');
#source('utilities/input_handler.dart');
+#source('utilities/double_input_handler.dart');
#source('utilities/complex_input_handler.dart');
#source('algorithms/sorting/quicksort.dart');
@@ -33,11 +34,10 @@
#source('math/hyperbolic.dart');
#source('math/logarithm.dart');
+#source('signals/waveforms.dart');
+
void main() {
- List<int> dataList = [75, 22, 84, 121, 16, 3, 67, 42, 17, 91];
- int order = 5;
- var topFive = rsel(dataList, order, true);
- print(topFive.value);
- var a = complex(1,1);
- print(a.string);
+ var sqwave = pulse(4);
+ print(sqwave.data.length);
+ sqwave.exportToWeb('local', 8080);
}
View
@@ -0,0 +1,141 @@
+/* ****************************************************** *
+ * Class _Waves: A simple waveform generator. *
+ * Library: ConvoLab (c) 2012 scribeGriff *
+ * ****************************************************** */
+//TODO needs some cleanup and error handling.
+//Generate square wave
+ConvoLabResults square([num cycles = 1, num amp = 1])
+ => new _Waves().generate('square', cycles, amp);
+
+//Generate triangle wave
+ConvoLabResults triangle([num cycles = 1, num amp = 1])
+ => new _Waves().generate('triangle', cycles, amp);
+
+//Generate ramp wave
+ConvoLabResults ramp([num cycles = 1, num amp = 1])
+ => new _Waves().generate('ramp', cycles, amp);
+
+//Generate pulse wave
+ConvoLabResults pulse([num cycles = 1, num amp = 1])
+ => new _Waves().generate('pulse', cycles, amp);
+
+//Generate voice wave
+ConvoLabResults sound([num cycles = 1, num amp = 1])
+ => new _Waves().generate('sound', cycles, amp);
+
+class _Waves {
+ final points;
+ List data, mark, space, trise, tfall;
+
+ _Waves()
+ : points = 512,
+ data = [],
+ mark = [],
+ space = [],
+ trise = [],
+ tfall = [];
+
+ ConvoLabResults generate(String waveform, num cycles, amp) {
+ //Square wave generator.
+ if (waveform == 'square') {
+ var minAmp = 0;
+ var tr, tf;
+ var edge = (points / cycles) * 0.05;
+ tr = edge > 4 ? edge.floor().toInt() : 4;
+ tf = edge > 4 ? edge.floor().toInt() : 4;
+ space.insertRange(0, ((points / (2 * cycles)).floor().toInt()) - tr, minAmp);
+ mark.insertRange(0, ((points / (2 * cycles)).floor().toInt()) - tf, amp);
+ for (var j = 1; j <= tr; j++) {
+ trise.add(((amp - minAmp) / tr) * j);
+ tfall.add(amp - ((amp - minAmp) / tf) * j);
+ }
+ for (var i = 0; i < cycles; i++) {
+ data
+ ..addAll(space)
+ ..addAll(trise)
+ ..addAll(mark)
+ ..addAll(tfall);
+ }
+ if (data.length < points) {
+ data.insertRange(data.length, points - data.length, minAmp);
+ }
+ //Triangle wave generator.
+ } else if (waveform == 'triangle') {
+ var minAmp = 0;
+ var tr, tf;
+ tr = (points / (2 * cycles)).floor().toInt() - 1;
+ tf = (points / (2 * cycles)).floor().toInt() - 1;
+ space.insertRange(0, 1, minAmp);
+ mark.insertRange(0, 1, amp);
+ for (var j = 1; j <= tr; j++) {
+ trise.add(((amp - minAmp) / tr) * j);
+ tfall.add(amp - ((amp - minAmp) / tf) * j);
+ }
+ for (var i = 0; i < cycles; i++) {
+ data
+ ..addAll(space)
+ ..addAll(trise)
+ ..addAll(mark)
+ ..addAll(tfall);
+ }
+ if (data.length < points) {
+ List temp = trise.getRange(0, points - data.length);
+ data.addAll(temp);
+ }
+ //Ramp generator.
+ } else if (waveform == 'ramp') {
+ var minAmp = 0;
+ var tr, tf;
+ var edge = (points / cycles) * 0.05;
+ tf = edge > 4 ? edge.floor().toInt() : 4;
+ tr = (points / (cycles)).floor().toInt() - tf - 2;
+ space.insertRange(0, 1, minAmp);
+ mark.insertRange(0, 1, amp);
+ for (var j = 1; j <= tr; j++) {
+ trise.add(((amp - minAmp) / tr) * j);
+ }
+ for (var j = 1; j <= tf; j++) {
+ tfall.add(amp - ((amp - minAmp) / tf) * j);
+ }
+ for (var i = 0; i < cycles; i++) {
+ data
+ ..addAll(space)
+ ..addAll(trise)
+ ..addAll(mark)
+ ..addAll(tfall);
+ }
+ if (data.length < points) {
+ List temp = trise.getRange(0, points - data.length);
+ data.addAll(temp);
+ }
+ //Pulse generator.
+ } else if (waveform == 'pulse') {
+ var minAmp = 0;
+ var tr, tf;
+ var edge = (points / cycles) * 0.05;
+ tr = edge > 4 ? edge.floor().toInt() : 4;
+ tf = edge > 4 ? edge.floor().toInt() : 4;
+ space.insertRange(0, (9 * (points / (10 * cycles)).floor().toInt()) - tr, minAmp);
+ mark.insertRange(0, ((points / (10 * cycles)).floor().toInt()) - tf, amp);
+ for (var j = 1; j <= tr; j++) {
+ trise.add(((amp - minAmp) / tr) * j);
+ tfall.add(amp - ((amp - minAmp) / tf) * j);
+ }
+ for (var i = 0; i < cycles; i++) {
+ data
+ ..addAll(space)
+ ..addAll(trise)
+ ..addAll(mark)
+ ..addAll(tfall);
+ }
+ if (data.length < points) {
+ data.insertRange(data.length, points - data.length, minAmp);
+ }
+ //Sound sample.
+ } else if (waveform == 'sound') {
+ String filename = "external/data/sound.txt";
+ data = new DoubleInputListHandler().prepareList(filename);
+ }
+ return new ConvoLabResults(data, cycles);
+ }
+}
@@ -30,7 +30,7 @@ class ComplexInputListHandler {
return _complexList;
}
- List<int> listFromFileSync(String filename) {
+ List<num> listFromFileSync(String filename) {
List<String> _buffin;
List<num> _buffer = [];
File fileHandle = new File(filename);
@@ -53,7 +53,7 @@ class ComplexInputListHandler {
}
}
- List<int> listFromFileAsync(String filename) {
+ List<num> listFromFileAsync(String filename) {
//Not implemented yet...
return(null);
}
@@ -0,0 +1,54 @@
+/* ****************************************************** *
+ * Double InputListHandler returns Double list *
+ * Library: ConvoLab (c) 2012 scribeGriff *
+ * ****************************************************** */
+
+class DoubleInputListHandler {
+ List prepareList(var fileOrList, [String syncOrAsync = 'sync']) {
+ List<num> _numList;
+ if (fileOrList is String) {
+ if (syncOrAsync == 'sync') {
+ _numList = listFromFileSync(fileOrList);
+ } else {
+ _numList = listFromFileAsync(fileOrList);
+ }
+ } else if (fileOrList is List) {
+ if (fileOrList.every(f(element) => element is num)) {
+ _numList = new List.from(fileOrList);
+ } else {
+ print("The input data is not formatted correctly.");
+ print("Elements must be type num or complex.");
+ return(null);
+ }
+ }
+ return _numList;
+ }
+
+ List<num> listFromFileSync(String filename) {
+ List<String> _buffin;
+ List<num> _buffer = [];
+ File fileHandle = new File(filename);
+ try {
+ _buffin = fileHandle.readAsLinesSync();
+ } catch(error) {
+ print("There was an error opening the file:\n$error");
+ return(null);
+ }
+ //If successfully read from file, try to parse the lines
+ //as doubles to _buffer and return.
+ try {
+ _buffin.forEach((element) {
+ if(!element.isEmpty()) _buffer.add(parseDouble(element.trim()));
+ });
+ return _buffer;
+ } catch(error) {
+ print("There was an error reading the input data:\n$error");
+ return(null);
+ }
+ }
+
+ List<num> listFromFileAsync(String filename) {
+ //Not implemented yet...
+ return(null);
+ }
+}

0 comments on commit ba18d31

Please sign in to comment.