-
Notifications
You must be signed in to change notification settings - Fork 0
/
callGaborator.js
129 lines (93 loc) · 3.78 KB
/
callGaborator.js
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
// function onRuntimeInitialized() {
const em_module = require('./wrap_gaborator.js');
// async function getWrapper() {
// const m = em_module();
// console.log(m.then);
// const module= await m;
// console.log(module);
// return module;
// }
// async function testGaborator() {
// const wrapper = await getWrapper();
// wrapper._sayHi();
// }
// testGaborator();
const width=1024;
const height=768;
const { createCanvas, loadImage } = require('canvas')
const canvas = createCanvas(width, height)
const ctx = canvas.getContext('2d')
ctx.globalCompositeOperation = 'lighten';
ctx.antialias = 'subpixel'
ctx.fillStyle = 'rgba(0,0,0,1)';
ctx.fillRect(0,0,canvas.width,canvas.height);
// ctx.strokeStyle='rgba(0,0,0,0)'
// Write "Awesome!"
// ctx.font = '30px Impact'
// ctx.rotate(0.1)
// ctx.fillText('Awesome!', 50, 100)
// // Draw line under text
// var text = ctx.measureText('Awesome!')
// ctx.strokeStyle = 'rgba(0,0,0,0.5)'
// ctx.beginPath()
// ctx.lineTo(50, 102)
// ctx.lineTo(50 + text.width, 102)
// ctx.stroke()
// const totalBands = 120;
function pixel(x,band, intensity,totalBands, totalSamples) {
const r = 1;
const g = r;
const b = r;
// console.log(intensity)
let a = Math.min(Math.max(0, Math.log(intensity*100.0+1.0 )),1);
if (a > 0.000) {
// console.log(a);
ctx.fillStyle = "rgba("+(r*255)+","+(g*255)+","+(b*255)+","+(a)+")";
// console.log("rgba("+(r*255)+","+(g*255)+","+(b*255)+","+(a)+")");
ctx.fillRect(x*width/totalSamples, band*height/totalBands, 5.0, height/totalBands);
}
}
// pixel(1,1,0,0,0);
var fs = require('fs');
var out = fs.createWriteStream(__dirname + '/text.png');
const loader = require('audio-loader')
const m = em_module();
m.then(({ThomashGaborator}) => {
// console.log(Object.keys(Module.ThomashGaborator))
// Module._analyze(Float32Array.from([0,1,0,0.5]));
console.log(ThomashGaborator);
let maxIntensity = -Infinity;
let minIntensity = Infinity;
loader("./media/modular.wav").then(audio => {
const gaborator= new ThomashGaborator(audio.sampleRate,48,20.0);
const data = audio.getChannelData(0);
const length = audio.length;
// console.log(data);
const numBands = gaborator.numBands();
// gaborator.analyze(data,function(mag,band,t) {
// // console.log("resuult",{real,imag,band,t});
// const intensity=mag*mag ;
// maxIntensity= Math.max(intensity,maxIntensity);
// minIntensity= Math.min(intensity,minIntensity);
// // numBands = Math.max(band,numBands)
// // console.log(t/length, band/72, 0,0,0,intensity)
// });
// console.log({maxIntensity,minIntensity,numBands});
gaborator.analyze(data,function(mag,band,t) {
// console.log("resuult",{real,imag,band,t});
const intensity=mag;
const normalizedIntensity= intensity;//(intensity-minIntensity)/(maxIntensity-minIntensity) ;
pixel(t, band, normalizedIntensity,numBands,length);
// console.log(t/length, band/72, 0,0,0,intensity)
});
var stream = canvas.pngStream();
stream.on('data', function(chunk){out.write(chunk); });
stream.on('end', function(){console.log('saved png'); });
console.log(data.length)
})
.catch(e => console.error(e));
// const data=new Float32Array(44100).fill(1.0) ;
})
// Module['onRuntimeInitialized'] = onRuntimeInitialized;
// em_module.ccall("sayHi"); // using ccall etc. also work
// console.log(em_module._daysInWeek()); // values can be returned, etc.