Pastebin
API
tools
faq
paste
Login
Sign up
Please fix the following errors:
New Paste
Syntax Highlighting
Chord thing, sorry for using an AI again for this as there could be some shoddy mistakes (still using my phone, gotta shower and eat food), I also like reading code from others that isn't mine but AIs don't easily impress me that much (too verbose, too simple, or doesn't think outside the box, can't correct certain mistakes that require visualization/hearing/runtime)… [code=javascript] //(16 Nov 2024 10:00) I asked o1 to write a chord progression interpreter. //I don't like using redundant spaces between tokens (but newlines and tab indentations are OK), it's better to be more concise and use ES6 tricks whenever possible. //(11:40) There were some issues with minor chords. Now it's “error: Cannot read properties of undefined (reading 'match')”… var PI=Math.PI,TAU=PI*2, noteNumbers={ "C":0, "C#":1,"Db":1, "D":2, "D#":3,"Eb":3, "E":4, "F":5, "F#":6,"Gb":6, "G":7, "G#":8,"Ab":8, "A":9, "A#":10,"Bb":10, "B":11 }; //parse a chord string into root note, modifiers, and bass note function parseChordString(chordStr){ /* //split the chord by slash to separate chord and bass note var parts=chordStr.split('/'), chordPart=parts[0], bassNote=parts[1]||null; */ //check if the chord contains a bass note var bassNoteMatch=chordStr.match(/\/([A-G][#b]?)$/), bassNote=null; if(bassNoteMatch){ bassNote=bassNoteMatch[1]; chordStr=chordStr.slice(0,chordStr.length-bassNoteMatch[0].length); //remove bass note part from chordStr } //extract the root note and modifiers from the chordStr var match=chordStr.match(/^([A-G][#b]?)(.*)$/); if(!match)throw new Error("Invalid chord: "+chordStr); var rootNote=match[1], modifiers=match[2]; return{ rootNote:rootNote, modifiers:modifiers, bassNote:bassNote }; } //function to parse chord modifiers into tokens function parseModifiers(modifiers){ let tokens=[], str=modifiers; while(str.length>0){ let matched=false, res; //match 'maj' with optional extension if(res=str.match(/^maj(7|9|11|13)?/)){ tokens.push('maj'); if(res[1])tokens.push(res[1]); str=str.substr(res[0].length); matched=true; } //match 'm' or 'min' with optional extension else if(res=str.match(/^m(in)?(7|9|11|13)?/)){ tokens.push('min'); if(res[2])tokens.push(res[2]); str=str.substr(res[0].length); matched=true; } //match 'dim' with optional '7' else if(res=str.match(/^dim(7)?/)){ tokens.push('dim'); if(res[1])tokens.push(res[1]); str=str.substr(res[0].length); matched=true; } //match 'aug' else if(res=str.match(/^aug/)){ tokens.push('aug'); str=str.substr(res[0].length); matched=true; } //match 'sus', 'sus2', 'sus4' else if(res=str.match(/^sus(2|4)?/)){ tokens.push('sus'+(res[1]||'')); str=str.substr(res[0].length); matched=true; } //match 'add' with optional alteration else if(res=str.match(/^add(#|b)?(\d+)/)){ tokens.push('add'); tokens.push((res[1]||'')+res[2]); str=str.substr(res[0].length); matched=true; } //match extensions like '13', '11', '9' else if(res=str.match(/^(13|11|9)/)){ tokens.push(res[1]); str=str.substr(res[1].length); matched=true; } //match alterations like '#5', 'b9' else if(res=str.match(/^([#b])(\d+)/)){ tokens.push(res[1]+res[2]); str=str.substr(res[0].length); matched=true; } //match '6/9' else if(res=str.match(/^6\/9/)){ tokens.push('6/9'); str=str.substr(4); matched=true; } //match '6', '7', '5' else if(res=str.match(/^(6|7|5)/)){ tokens.push(res[1]); str=str.substr(1); matched=true; } //skip unrecognized characters else{ str=str.substr(1); } } return tokens; } //helper function to get interval from scale degree var intervalsMap={1:0,2:2,3:4,4:5,5:7,6:9,7:11,9:14,11:17,13:21}; function getIntervalFromDegree(degree){return intervalsMap[degree]||0;} //function to get chord intervals based on modifiers function getChordIntervals(modifiers){ var tokens=parseModifiers(modifiers), intervals=[]; //start with basic triad var chordQuality='major'; //default if(tokens.includes('maj')){ chordQuality='major'; }else if(tokens.includes('min')){ chordQuality='minor'; }else if(tokens.includes('dim')){ chordQuality='diminished'; }else if(tokens.includes('aug')){ chordQuality='augmented'; } //build the basic triad intervals if(chordQuality=='major'){ intervals.push(0,4,7); }else if(chordQuality=='minor'){ intervals.push(0,3,7); }else if(chordQuality=='diminished'){ intervals.push(0,3,6); }else if(chordQuality=='augmented'){ intervals.push(0,4,8); } //handle sevenths if(tokens.includes('maj') && tokens.includes('7')){ intervals.push(11); }else if(tokens.includes('7')){ intervals.push(10); } //handle extensions if(tokens.includes('9')){ intervals.push(14); } if(tokens.includes('11')){ intervals.push(17); } if(tokens.includes('13')){ intervals.push(21); } //handle '6' and '6/9' if(tokens.includes('6/9')){ intervals.push(9,14); }else if(tokens.includes('6')){ intervals.push(9); } //handle added tones if(tokens.includes('add')){ tokens.filter(t=>t.startsWith('#')||t.startsWith('b')||/^\d+$/.test(t)).forEach(t=>{ var number=parseInt(t.replace('#','').replace('b','')), interval = getIntervalFromDegree(number); if(t.startsWith('#'))interval++; if(t.startsWith('b'))interval--; intervals.push(interval); }); } //handle suspensions if(tokens.includes('sus2')){ //replace the third with major 2nd var index=intervals.indexOf(chordQuality=='major'?4:3); if(index>=0)intervals[index]=2; }else if(tokens.includes('sus4')){ //replace the third with perfect 4th var index=intervals.indexOf(chordQuality=='major'?4:3); if(index>=0)intervals[index]=5; }else if(tokens.includes('sus')){ //replace the third with perfect 4th var index=intervals.indexOf(chordQuality=='major'?4:3); if(index>=0)intervals[index]=5; } //handle alterations tokens.forEach(t=>{ if(t=='#5'){ //replace the fifth with augmented fifth var index=intervals.indexOf(7); if(index>=0)intervals[index]=8; }else if(t=='b5'){ //replace the fifth with diminished fifth var index=intervals.indexOf(7); if(index>=0)intervals[index]=6; }else if(t=='#9'){ intervals.push(15); //sharp ninth }else if(t=='b9'){ intervals.push(13); //flat ninth }else if(t=='#11'){ intervals.push(18); //sharp eleventh }else if(t=='b13'){ intervals.push(20); //flat thirteenth } }); //remove duplicates and sort intervals intervals=intervals.filter((v,i,a)=>a.indexOf(v)===i); intervals.sort((a,b)=>a-b); return intervals; } function note(n,base=440,keys=12){return base*Math.pow(2,n/keys);} function sine(x){return Math.sin(TAU*x);} function pulse(x){return x%1<.5?-1:1;} //limit frequency within a given range function limitFrequency(freq,minFreq,maxFreq){ while(freq<minFreq)freq*=2; while(freq>maxFreq)freq/=2; return freq; } var chordDuration=2, //seconds per chord //chords="Amaj7sus2/Ab Gmaj7sus2 C69#11 Bm11 Ab7#5b9 Dbm11 Cmaj7b5/6 Abm11 F#m11".split(" "), chords="Gb7 Gmmaj7 Emmaj9 Ab9addb13/C Gbaug Bmaj9 Abm11 Dbm11 Gb7/#11 Dbmaj9sus2 Eb13sus4 Dbm9".split(" "), //chords="Am7 Dm9 Em11 Fmaj7 G7 Cmaj9".split(" "), totalChords = chords.length; return function dsp(t){ var out=0, chordIndex=Math.floor(t/chordDuration)%totalChords, chordTime=t%chordDuration, chordStr=chords[chordIndex], chord=parseChordString(chordStr), rootNote=chord.rootNote, modifiers=chord.modifiers, bassNote=chord.bassNote||chord.rootNote, intervals=getChordIntervals(modifiers), rootNoteNumber=noteNumbers[rootNote], //get the semitone offset from A4 (note number 9) semitoneOffset=rootNoteNumber-9, //calculate frequencies and sum the sine waves numNotes=intervals.length+1; //number of notes in the chord including bass for(var i=0;i<intervals.length;i++){ var n=semitoneOffset+intervals[i], //total semitone offset from A4 freq=limitFrequency(note(n),220,880); out+=sine(freq*t); } //add the bass note an octave lower if(bassNote){ var bassNoteNumber=noteNumbers[bassNote], bassSemitoneOffset=bassNoteNumber-9-12, //bass an octave lower bassFreq=note(bassSemitoneOffset); out+=sine(bassFreq*t); } //normalize output out=out/numNotes; return out; } [/code] >[b]Error[/b] >Your post has too many lines. Fuck off.
Optional Paste Settings
Category:
None
Cryptocurrency
Cybersecurity
Fixit
Food
Gaming
Haiku
Help
History
Housing
Jokes
Legal
Money
Movies
Music
Pets
Photo
Science
Software
Source Code
Spirit
Sports
Travel
TV
Writing
Tags:
Syntax Highlighting:
None
Bash
C
C#
C++
CSS
HTML
JSON
Java
JavaScript
Lua
Markdown (PRO members only)
Objective C
PHP
Perl
Python
Ruby
Swift
4CS
6502 ACME Cross Assembler
6502 Kick Assembler
6502 TASM/64TASS
ABAP
AIMMS
ALGOL 68
APT Sources
ARM
ASM (NASM)
ASP
ActionScript
ActionScript 3
Ada
Apache Log
AppleScript
Arduino
Asymptote
AutoIt
Autohotkey
Avisynth
Awk
BASCOM AVR
BNF
BOO
Bash
Basic4GL
Batch
BibTeX
Blitz Basic
Blitz3D
BlitzMax
BrainFuck
C
C (WinAPI)
C Intermediate Language
C for Macs
C#
C++
C++ (WinAPI)
C++ (with Qt extensions)
C: Loadrunner
CAD DCL
CAD Lisp
CFDG
CMake
COBOL
CSS
Ceylon
ChaiScript
Chapel
Clojure
Clone C
Clone C++
CoffeeScript
ColdFusion
Cuesheet
D
DCL
DCPU-16
DCS
DIV
DOT
Dart
Delphi
Delphi Prism (Oxygene)
Diff
E
ECMAScript
EPC
Easytrieve
Eiffel
Email
Erlang
Euphoria
F#
FO Language
Falcon
Filemaker
Formula One
Fortran
FreeBasic
FreeSWITCH
GAMBAS
GDB
GDScript
Game Maker
Genero
Genie
GetText
Go
Godot GLSL
Groovy
GwBasic
HQ9 Plus
HTML
HTML 5
Haskell
Haxe
HicEst
IDL
INI file
INTERCAL
IO
ISPF Panel Definition
Icon
Inno Script
J
JCL
JSON
Java
Java 5
JavaScript
Julia
KSP (Kontakt Script)
KiXtart
Kotlin
LDIF
LLVM
LOL Code
LScript
Latex
Liberty BASIC
Linden Scripting
Lisp
Loco Basic
Logtalk
Lotus Formulas
Lotus Script
Lua
M68000 Assembler
MIX Assembler
MK-61/52
MPASM
MXML
MagikSF
Make
MapBasic
Markdown (PRO members only)
MatLab
Mercury
MetaPost
Modula 2
Modula 3
Motorola 68000 HiSoft Dev
MySQL
Nagios
NetRexx
Nginx
Nim
NullSoft Installer
OCaml
OCaml Brief
Oberon 2
Objeck Programming Langua
Objective C
Octave
Open Object Rexx
OpenBSD PACKET FILTER
OpenGL Shading
Openoffice BASIC
Oracle 11
Oracle 8
Oz
PARI/GP
PCRE
PHP
PHP Brief
PL/I
PL/SQL
POV-Ray
ParaSail
Pascal
Pawn
Per
Perl
Perl 6
Phix
Pic 16
Pike
Pixel Bender
PostScript
PostgreSQL
PowerBuilder
PowerShell
ProFTPd
Progress
Prolog
Properties
ProvideX
Puppet
PureBasic
PyCon
Python
Python for S60
QBasic
QML
R
RBScript
REBOL
REG
RPM Spec
Racket
Rails
Rexx
Robots
Roff Manpage
Ruby
Ruby Gnuplot
Rust
SAS
SCL
SPARK
SPARQL
SQF
SQL
SSH Config
Scala
Scheme
Scilab
SdlBasic
Smalltalk
Smarty
StandardML
StoneScript
SuperCollider
Swift
SystemVerilog
T-SQL
TCL
TeXgraph
Tera Term
TypeScript
TypoScript
UPC
Unicon
UnrealScript
Urbi
VB.NET
VBScript
VHDL
VIM
Vala
Vedit
VeriLog
Visual Pro Log
VisualBasic
VisualFoxPro
WHOIS
WhiteSpace
Winbatch
XBasic
XML
XPP
Xojo
Xorg Config
YAML
YARA
Z80 Assembler
ZXBasic
autoconf
jQuery
mIRC
newLISP
q/kdb+
thinBasic
Paste Expiration:
Never
Burn after read
10 Minutes
1 Hour
1 Day
1 Week
2 Weeks
1 Month
6 Months
1 Year
Paste Exposure:
Public
Unlisted
Private
Folder:
(members only)
Password
NEW
Enabled
Disabled
Burn after read
NEW
Paste Name / Title:
Create New Paste
Hello
Guest
Sign Up
or
Login
Sign in with Facebook
Sign in with Twitter
Sign in with Google
You are currently not logged in, this means you can not edit or delete anything you paste.
Sign Up
or
Login
Public Pastes
.zshrc
Bash | 3 min ago | 1.23 KB
Older node trading
CSS | 18 min ago | 0.99 KB
FREE COINS METHOD
CSS | 18 min ago | 0.99 KB
How to hack an exchange
CSS | 18 min ago | 0.99 KB
Got $12,000 from this method
CSS | 18 min ago | 0.99 KB
This month smells like money
CSS | 18 min ago | 0.99 KB
Instant Profits Method
CSS | 18 min ago | 0.99 KB
Custom em_printables_pdf_get_invoice_number f...
15 hours ago | 0.24 KB
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the
Cookies Policy
.
OK, I Understand
Not a member of Pastebin yet?
Sign Up
, it unlocks many cool features!