hi
i'm zachary canfer and this is waving
and repetitive repetitive repetitive
music over quarantine i've been bored
and i found this android app that has a
bunch of mini games
one of which lets you compose music
and it works but i
want a little bit more functionality
than it offers it's not very flexible
so i thought what if i made this
what and what programs could i make this
in that are really flexible are really
customizable
emacs so i looked into it and
emacs can play sounds right if you hit
control g a couple times you'll hear
like an error tone
and it turns out that that is actually
playing a wave file
but what's a wave file
well it turns out that wave is a musical
file format
or really an annoying file format
so data in it can be an unsigned integer
or a science integer it's not consistent
and it's little ending by default which
is not the way i like to think about it
now you can set a wav file to be big
endian but if you do that
emacs can't play it so little ending it
is
there's also duplicate data fields
here are some fields that are fine but
then there's a fourth field that's
calculated based on multiplying two of
the other ones together
and then there's another data field
that's you multiply those three ones
together so it's just repetitive and
unnecessary but you have to do it or
it's not a valid wave file
also the last part of the file is
described as data
or as one website i found said
the actual sound data now
i don't know about you but when i see
that i think
what is a data it turns out
that sound is just a wave and
the data is just a bunch of measurements
of the height of that wave forming each
sample
so this wave starts at 8 goes 9 11 13
14 15 and then back down and if you just
take those measurements those numbers
put them in a file that's all your data
is
all right let's go to a demo of my
program
so this is what z music looks like the
blue highlighted row is a single beat
there's 16 of them in this z music file
and each dash in the row
is a single note increasing starting
really low all the way on the left and
going up
as we go to the right so if we started
playing
we won't hear anything but we'll see the
highlighted beat is the currently
playing one
and we see that it loops so we can stop
it
and now we can click to add some notes
even more than one note at the same time
works
and we can even add notes while it's
[Applause]
playing
[Applause]
okay here are some other features that i
didn't have time to demo
so you can save the music to a file and
this is interesting because normally
if there's no note in a beat we just
don't play that beat
but if you're writing to a file you have
to put something in
so when it's playing it knows to not
make a sound there
we can also use different scales we're
using the minor pentatonic
in the demo but you can use the major
scale the minor scale
or anything else and there's also
keyboard support but it sounds really
bad
and i'll explain why later
here are some things i learned while
writing z music
emacs has buttons which are great as
long
as you don't put two of them right next
to each other so if you do that
mousing over one of them highlights both
of them
now that's because a button is really
just a series of characters with a text
property to highlight them
so the fix is you put another character
between the two buttons
then mousing over one of them only
highlights the one you want
but even this doesn't work really great
for z music
because the music has a lot of very
small buttons in a row
so it's really easy to accidentally put
your cursor over the space
and click on that instead of the button
so i looked into unicode and i found
this character called
a zero width space so we should be able
to put that between buttons and not be
able to accidentally click on it
unfortunately a zeroth space isn't
actually zero width
if we put a hundred of them between two
other characters you can see there's
space there
and i think what's happening is the
space is zero width
but then emax put uses one pixel between
each pair of characters
for the cursor so it's almost zero width
some ways to play sound that don't quite
work play sound plays music
but it blocks you can't do things like
set other notes or even pause the music
and if you throw it into async.el it's
silent and i don't know why
so the solution i went with is taking
that wav file
ran into the file system and then
shelling out to a native executable to
play the sound
and that works fine as long as you only
do it once
because if you do it a couple times at
the same time like if you have a chord
and you want to play three notes
simultaneously
you get this weird interference and
that's actually why the keyboard from
before didn't work
also side effects have this unexpected
impact
uh when you saw the demo it was running
pretty smoothly
but if i just add one message statement
every beat for
debugging purposes i was getting lag and
jitter
here's the one thing i learned about
music theory music theory
is not easy to program
i was looking around to see what
concepts we can use to code the scales
to code the
notes the first thing that i saw is
scale degrees
and this when i looked into it you don't
want to program in scale degrees
so you see we have the first second
third fourth fifth sixth seventh but
then it wraps around that octave up
is also a first and that's because both
of those notes are c
uh so that didn't work and also you
couldn't really easily
specify a flat or sharp you could say
a sharp third uh or
you know a flat seventh or whatever but
then you kind of have these two pieces
of data that indicate the note and i
didn't i didn't love that
so i looked a little looked again and i
found intervals
and then i thought about it and you
don't really want to program in
intervals either
it fixes some of the problems with scale
degrees you see
all the way on the right you have an
octave so you wrap to 8 and you
go 9 10 and that works but you solve the
same problem you see you have a major
third but below we also have a minor
third so you saw that problem of having
two pieces of information
uh so i thought about it music's really
frequencies like an a is 440 hertz
so at a low level that's what we're
going to do we're just going to use
frequencies
and then at the one level above that uh
that's a little bit easier for humans to
think about
we're going to use semitones up from the
root which is kind of like scale degrees
but instead of just counting each note
as one more we're going to say how many
semitones up it is
so if there's a sharp between two notes
that's going to be two steps up instead
of just one
and then we translate those two
frequencies so your a is 440 hertz
another note might be
613.5
or whatever and we that's we use the low
level to play
some future work i have i want to add
some drums
i want to make that keyboard actually
work uh
and uh computers and synthesizers are
the only place you hear a pure sine wave
like the one we have here
so i want to add overtones or other
octaves above it uh just to make it
sound a little bit more realistic
i've put notes references in the source
code up at zck dot me slash emacs conf
2020 i'm one of the organizers of emacs
nyc
check that out and if you take a look
or have any thoughts i'd love to hear
them and
thanks so much for coming to my talk