-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
338 lines (289 loc) Β· 10.3 KB
/
main.ts
File metadata and controls
338 lines (289 loc) Β· 10.3 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/usr/bin/env -S deno run -A
/**
* Code DJ - Ambient music generator based on git repo activity
*
* Maps git events to musical elements:
* - Commit velocity β tempo (more commits = faster)
* - File types β instruments (ts=synth, rs=bass, md=pad)
* - Insertions β higher notes, deletions β lower notes
* - Merge conflicts β dissonant chords
* - CI success β celebration sound
* - CI failure β sad trombone
*/
const SAMPLE_RATE = 44100;
const DURATION = 0.3; // seconds per note
// Musical scales
const MAJOR_SCALE = [0, 2, 4, 5, 7, 9, 11]; // C major intervals
const MINOR_SCALE = [0, 2, 3, 5, 7, 8, 10]; // C minor intervals
const BASE_FREQ = 261.63; // C4
interface GitStats {
commits: number;
insertions: number;
deletions: number;
files: string[];
hasConflicts: boolean;
authors: string[];
}
interface Note {
freq: number;
duration: number;
volume: number;
waveform: 'sine' | 'square' | 'sawtooth' | 'triangle';
}
// Generate a sine wave for a note
function generateTone(note: Note): Float32Array {
const samples = Math.floor(SAMPLE_RATE * note.duration);
const buffer = new Float32Array(samples);
for (let i = 0; i < samples; i++) {
const t = i / SAMPLE_RATE;
let sample = 0;
switch (note.waveform) {
case 'sine':
sample = Math.sin(2 * Math.PI * note.freq * t);
break;
case 'square':
sample = Math.sign(Math.sin(2 * Math.PI * note.freq * t));
break;
case 'sawtooth':
sample = 2 * (t * note.freq - Math.floor(0.5 + t * note.freq));
break;
case 'triangle':
sample = Math.abs(4 * (t * note.freq - Math.floor(t * note.freq + 0.5))) - 1;
break;
}
// Apply envelope (attack/decay)
const envelope = Math.min(1, i / (samples * 0.1)) * Math.min(1, (samples - i) / (samples * 0.3));
buffer[i] = sample * note.volume * envelope;
}
return buffer;
}
// Convert scale degree to frequency
function scaleToFreq(degree: number, scale: number[], octaveShift = 0): number {
const octave = Math.floor(degree / scale.length);
const note = scale[((degree % scale.length) + scale.length) % scale.length];
return BASE_FREQ * Math.pow(2, (note + (octave + octaveShift) * 12) / 12);
}
// Get waveform based on file extension
function fileToWaveform(file: string): 'sine' | 'square' | 'sawtooth' | 'triangle' {
const ext = file.split('.').pop()?.toLowerCase();
switch (ext) {
case 'ts':
case 'js':
return 'sine'; // smooth synth for JS/TS
case 'rs':
case 'go':
return 'square'; // punchy bass for systems langs
case 'md':
case 'txt':
return 'triangle'; // soft pad for docs
default:
return 'sawtooth'; // edgy for everything else
}
}
// Analyze git log and generate stats
async function getGitStats(repoPath: string, since = '1 hour ago'): Promise<GitStats> {
const cmd = new Deno.Command('git', {
args: ['log', `--since="${since}"`, '--pretty=format:%H|%an', '--numstat'],
cwd: repoPath,
stdout: 'piped',
stderr: 'piped',
});
const output = await cmd.output();
const text = new TextDecoder().decode(output.stdout);
const lines = text.trim().split('\n').filter(l => l);
const commits = lines.filter(l => l.includes('|')).length;
const authors = [...new Set(lines.filter(l => l.includes('|')).map(l => l.split('|')[1]))];
let insertions = 0;
let deletions = 0;
const files: string[] = [];
for (const line of lines) {
if (!line.includes('|') && line.trim()) {
const parts = line.split('\t');
if (parts.length === 3) {
insertions += parseInt(parts[0]) || 0;
deletions += parseInt(parts[1]) || 0;
files.push(parts[2]);
}
}
}
// Check for merge conflicts
const statusCmd = new Deno.Command('git', {
args: ['status', '--porcelain'],
cwd: repoPath,
stdout: 'piped',
});
const statusOutput = await statusCmd.output();
const statusText = new TextDecoder().decode(statusOutput.stdout);
const hasConflicts = statusText.includes('UU ') || statusText.includes('AA ');
return { commits, insertions, deletions, files: [...new Set(files)], hasConflicts, authors };
}
// Generate a chord (multiple notes at once)
function generateChord(notes: Note[]): Float32Array {
const maxDuration = Math.max(...notes.map(n => n.duration));
const totalSamples = Math.floor(SAMPLE_RATE * maxDuration);
const mixed = new Float32Array(totalSamples);
for (const note of notes) {
const tone = generateTone(note);
for (let i = 0; i < tone.length && i < mixed.length; i++) {
mixed[i] += tone[i];
}
}
// Normalize
const max = Math.max(...mixed.map(Math.abs));
if (max > 0) {
for (let i = 0; i < mixed.length; i++) {
mixed[i] /= max * 1.5; // leave headroom for mixing
}
}
return mixed;
}
// Generate music based on git stats - now creates a longer sequence
function generateMusic(stats: GitStats): Float32Array {
const scale = stats.hasConflicts ? MINOR_SCALE : MAJOR_SCALE;
// Base tempo on commit count (more commits = shorter notes)
const noteDuration = Math.max(0.15, 0.4 - stats.commits * 0.03);
// Create a chord progression based on activity
const progressions = stats.hasConflicts
? [[0, 2, 4], [5, 0, 2], [3, 5, 0], [4, 6, 1]] // darker progression
: [[0, 2, 4], [3, 5, 0], [4, 6, 1], [0, 2, 4]]; // happy progression
const allSamples: Float32Array[] = [];
const numBars = 4;
const chordsPerBar = 4;
for (let bar = 0; bar < numBars; bar++) {
for (let chord = 0; chord < chordsPerBar; chord++) {
const notes: Note[] = [];
const prog = progressions[chord % progressions.length];
// Get file types for this beat
const fileIdx = (bar * chordsPerBar + chord) % Math.max(1, stats.files.length);
const file = stats.files[fileIdx] || 'unknown.ts';
const waveform = fileToWaveform(file);
// Add chord tones
for (const degree of prog) {
const ratio = stats.insertions / (stats.insertions + stats.deletions + 1);
const octaveShift = ratio > 0.6 ? 1 : 0; // higher octave if more insertions
notes.push({
freq: scaleToFreq(degree, scale, octaveShift),
duration: noteDuration,
volume: 0.25,
waveform,
});
}
// Add bass on beats 1 and 3
if (chord % 2 === 0) {
notes.push({
freq: scaleToFreq(prog[0], scale, -1),
duration: noteDuration * 1.5,
volume: 0.35,
waveform: 'square',
});
}
// Add high arpeggio note based on commit activity
if (stats.commits > 0 && (bar + chord) % 3 === 0) {
notes.push({
freq: scaleToFreq(prog[bar % prog.length] + 7, scale, 1),
duration: noteDuration * 0.5,
volume: 0.15,
waveform: 'sine',
});
}
// If conflicts, add occasional dissonant note
if (stats.hasConflicts && chord === 2) {
notes.push({
freq: scaleToFreq(6, scale) * 1.06, // detuned
duration: noteDuration * 0.7,
volume: 0.2,
waveform: 'sawtooth',
});
}
allSamples.push(generateChord(notes));
}
}
// Concatenate all samples
const totalLength = allSamples.reduce((sum, s) => sum + s.length, 0);
const result = new Float32Array(totalLength);
let offset = 0;
for (const samples of allSamples) {
result.set(samples, offset);
offset += samples.length;
}
return result;
}
// Convert Float32Array to WAV format
function toWav(samples: Float32Array): Uint8Array {
const buffer = new ArrayBuffer(44 + samples.length * 2);
const view = new DataView(buffer);
// WAV header
const writeString = (offset: number, str: string) => {
for (let i = 0; i < str.length; i++) {
view.setUint8(offset + i, str.charCodeAt(i));
}
};
writeString(0, 'RIFF');
view.setUint32(4, 36 + samples.length * 2, true);
writeString(8, 'WAVE');
writeString(12, 'fmt ');
view.setUint32(16, 16, true); // chunk size
view.setUint16(20, 1, true); // PCM
view.setUint16(22, 1, true); // mono
view.setUint32(24, SAMPLE_RATE, true);
view.setUint32(28, SAMPLE_RATE * 2, true); // byte rate
view.setUint16(32, 2, true); // block align
view.setUint16(34, 16, true); // bits per sample
writeString(36, 'data');
view.setUint32(40, samples.length * 2, true);
// Convert samples to 16-bit PCM
for (let i = 0; i < samples.length; i++) {
const s = Math.max(-1, Math.min(1, samples[i]));
view.setInt16(44 + i * 2, s * 0x7FFF, true);
}
return new Uint8Array(buffer);
}
// Main
async function main() {
const repoPath = Deno.args[0] || '.';
const since = Deno.args[1] || '1 hour ago';
console.log(`π΅ Code DJ - Generating music from git activity`);
console.log(` Repo: ${repoPath}`);
console.log(` Since: ${since}`);
console.log();
const stats = await getGitStats(repoPath, since);
console.log(`π Git Stats:`);
console.log(` Commits: ${stats.commits}`);
console.log(` Insertions: ${stats.insertions}`);
console.log(` Deletions: ${stats.deletions}`);
console.log(` Files changed: ${stats.files.length}`);
console.log(` Authors: ${stats.authors.join(', ') || 'none'}`);
console.log(` Conflicts: ${stats.hasConflicts ? 'β οΈ YES' : 'β
No'}`);
console.log();
if (stats.commits === 0 && stats.files.length === 0) {
console.log('π No recent activity - generating ambient silence...');
// Generate a quiet ambient tone
const ambient = generateTone({
freq: scaleToFreq(0, MAJOR_SCALE),
duration: 2,
volume: 0.1,
waveform: 'triangle',
});
const wav = toWav(ambient);
await Deno.writeFile('/tmp/code-dj/output.wav', wav);
} else {
console.log('πΉ Generating music...');
const music = generateMusic(stats);
const wav = toWav(music);
await Deno.writeFile('/tmp/code-dj/output.wav', wav);
}
console.log(`β
Saved to /tmp/code-dj/output.wav`);
// Try to play it
try {
const playCmd = new Deno.Command('aplay', {
args: ['/tmp/code-dj/output.wav'],
stdout: 'null',
stderr: 'null',
});
await playCmd.output();
console.log('π Playing...');
} catch {
console.log('π‘ Install aplay to hear the music, or open output.wav');
}
}
main();