-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
79 lines (62 loc) · 2.25 KB
/
main.js
File metadata and controls
79 lines (62 loc) · 2.25 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
import { Configuration, OpenAIApi } from 'openai';
const audioContext = new AudioContext();
const makePrompt = async () => {
const length = '4';
const mood = 'melancholic';
const openai = new OpenAIApi(
new Configuration({
apiKey: '<SECRET-KEY>',
})
);
const completion = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: [
{
role: 'user',
content: `Generate a ${mood} chord progression with ${length} chords, with each chord represented as a note and length separated by a space in this format 'C 1, D 2, E 1, D 1'`
}
],
temperature: 1,
});
playNotes(
parseNotes(completion.data.choices[0].message.content)
);
}
const parseNotes = (string) => {
// string ~ 'C 1, F 1.5, G 0.5'
const notesArray = []
for (const str of string.split(', ')) {
const [note, length] = str.split(' ');
notesArray.push({note, length});
}
return notesArray;
}
const playNotes = (chordProgression) => {
let offset = 0;
// Loop through the chord progression and play each chord
chordProgression.forEach((chord) => {
// Create a new OscillatorNode to generate the sound wave for the current chord
const oscillator = audioContext.createOscillator();
// Set the frequency of the oscillator based on the current chord
oscillator.frequency.setValueAtTime(getFrequency(chord.note), audioContext.currentTime);
// Connect the oscillator to the ConvolverNode
oscillator.connect(audioContext.destination);
// Start the oscillator
oscillator.start(audioContext.currentTime + offset);
// Stop the oscillator after the duration of the current chord
oscillator.stop(audioContext.currentTime + offset + Number(chord.length));
// Add to the offset
offset += Number(chord.length);
});
}
// Function to get the frequency of a given note (in Hz)
const getFrequency = (note) => {
const octave = 1;
const noteNames = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B'];
const noteIndex = noteNames.indexOf(note);
const isMinor = note.endsWith('m');
const interval = isMinor ? [0, 3, 7, 10] : [0, 4, 7, 10];
const frequency = 220 * Math.pow(2, (noteIndex - 9 + (octave * 12) + interval[0]) / 12);
return frequency;
}
makePrompt();