TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

How to create minimal music with code in any programming language

265 pointsby pmarinover 4 years ago

19 comments

elihuover 4 years ago
A random suggestion to make for nicer, more interesting music: the article takes pitches from a table of equal tempered note frequencies relative to A440:<p>&gt; For example, here’s a familiar riff “E B D E D B A B”. It uses only 4 notes - D and E from one octave and A + B from the other, lower octave. If we look at the note frequency table, the pitches of those notes in octaves 5 and 4 would be 659.2Hz (E), 587.3Hz (D), 440Hz (A) and 493.8Hz (B).<p>Instead of equal tempered pitches (which are generated by repeatedly multiplying by the 12th root of 2), use pitches that form whole number ratios with respect to the root of your key.<p>A decent 12-note chromatic scale would be something like 1:1, 16:15, 9:8, 6:5, 5:4, 4:3, 45:32, 3:2, 8:5, 5:3, 15:8, and 2:1. So, for instance if your root is C=256 hz, you&#x27;d have 256 hz for C, (256<i>16)&#x2F;15 for C#, (256</i>9)&#x2F;8 for D, and so on. This is a just intonation (JI) scale. An advantage of JI is that the musical intervals blend with each other better, whereas the advantage of equal temperament (ET) is that you can use any note as the root and change keys on the fly without causing problems. If you&#x27;re doing electronic music, though, there&#x27;s a lot less reason to stick with ET: you can always just multiply your whole tuning table by a constant if you want to switch key.<p>JI scales can be made with any ratios: the above maps well to most conventional music, but there&#x27;s no rule that you have to limit yourself to 12 steps per octave. Using the harmonic series is another option, or adding in ratios that have a 7 in them.
评论 #24949058 未加载
评论 #24948937 未加载
评论 #24949355 未加载
评论 #24949647 未加载
评论 #24950086 未加载
munificentover 4 years ago
This is a great article! I&#x27;ve been interested in music software and hardware for decades and my lockdown hobby has been finally spending the time to really dig into it. It is both very challenging and very rewarding.<p>If you want to go a little deeper than the article, here&#x27;s a few more notes:<p>The way the author generates the sawtooth and square waves is &quot;analytically&quot;. That means treating them like a function of time and simply calculating the waveform position each point in time. As you can see, it&#x27;s really simple. Unfortunately, it also introduces a ton of aliasing and will sound pretty harsh, especially at higher pitches. If you&#x27;re familiar with nearest-neighbor sampling in image resizing, think about how nasty and chunky the resulting image looks when you scale it down using nearest neighbor. Analytic waveforms do the equivalent thing for audio.<p>Fixing that problem is surprisingly hard. It&#x27;s a lot like texture rendering in games where there&#x27;s a bunch of filtering, tricks, and hacks you can do to make things look smooth without burning a ton of CPU cycles.<p>---<p>The clever trick the author uses to simulate strings is: <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Karplus%E2%80%93Strong_string_synthesis" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Karplus%E2%80%93Strong_string_...</a><p>---<p>The low-pass filter they show is a simple first-order 6 dB&#x2F;octave digital low-pass filter. Filtering is fundamental to electronic music. The majority of synthesizers you hear in electronic music use &quot;subtractive synthesis&quot; which means to start with the simple sawtooth and square waves introduced early in the article and use filters to tame their harsh overtones. I find the math behind filter design, especially the moving filters used for synths, really difficult, but also interesting.
nijaruover 4 years ago
Small nitpick: CD quality is in fact 44.1khz. The nyquist theorem essentially says you need to use a sampling rate twice as high as the highest frequencies you want to record and reproduce. The article gets the gist of this right, just the cd quality numbers wrong. Izotope has a really good article that goes into digital to analog conversion, sampling rates, and the history behind them.<p><a href="https:&#x2F;&#x2F;www.izotope.com&#x2F;en&#x2F;learn&#x2F;digital-audio-basics-sample-rate-and-bit-depth.html" rel="nofollow">https:&#x2F;&#x2F;www.izotope.com&#x2F;en&#x2F;learn&#x2F;digital-audio-basics-sample...</a>
评论 #24949039 未加载
brody_hamerover 4 years ago
Thank you! For weeks now I&#x27;ve been thinking about how handy it&#x27;d be if I could audibly &#x27;listen&#x27; to the output of a command. I had no idea it was as easy as piping it to aplay.<p>`tail -f output.txt | aplay`<p>I know this is a lame takeaway, but I&#x27;m just so happy to learn about this.
评论 #24950975 未加载
评论 #24949170 未加载
评论 #24952284 未加载
评论 #24952369 未加载
contravariantover 4 years ago
This somewhat related talk about functional composition (in lisp) is also quite interesting: <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=jyNqHsN3pEc" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=jyNqHsN3pEc</a>
评论 #24948525 未加载
aurelian15over 4 years ago
The &quot;code poem&quot; at the beginning of the post reminds me of the &quot;Bit Shift Variations in C-Minor&quot; [1] by Robert S K Miles (chiptune music in 214 bytes of C; featured in the computerphile video &quot;Code Golf &amp; the Bitshift Variations&quot;).<p>[1] <a href="http:&#x2F;&#x2F;txti.es&#x2F;bitshiftvariationsincminor" rel="nofollow">http:&#x2F;&#x2F;txti.es&#x2F;bitshiftvariationsincminor</a>
评论 #24948323 未加载
martttover 4 years ago
Pete Elmore also recently wrote an interesting post about Bytebeat and Algorithmic Composition:<p><a href="http:&#x2F;&#x2F;debu.gs&#x2F;entries&#x2F;making-music-with-computers-two-unconventional-approaches" rel="nofollow">http:&#x2F;&#x2F;debu.gs&#x2F;entries&#x2F;making-music-with-computers-two-uncon...</a>
zengargoyleover 4 years ago
My first hacking as it were was loading the Apple ][ game Lemonade and then writing my own programs. Because Lemonade loaded a bit of code that played music and I could figure out how to play notes and durations once the magic code was loaded but not how to do the magic code myself. It took quite a while and a book on 6502 assembly and my high-school&#x27;s Franklin Apple clones (Franklin had a built in assembler in the ROM) to figure out how to make that speaker beep actually play notes. My first great hack was finally being able to write a program that played music on the other Bell &amp; Howell Black Apple machines. Then I got an Amiga and that musical note playing became trivial. Article seems a bit bogus by not going down to speaker-on&#x2F;speaker-off and delay loops.
评论 #24948845 未加载
FullyFunctionalover 4 years ago
The article and this whole thread is awesome; there’s so much to learn.<p>My only contribution is to point out that the late Hudak was very interested in making music with software and spent a significant portion of The Haskell School of Expression, <a href="https:&#x2F;&#x2F;books.google.com&#x2F;books&#x2F;about&#x2F;The_Haskell_School_of_Expression.html?id=lQbth9j5j9oC&amp;printsec=frontcover&amp;source=kp_read_button" rel="nofollow">https:&#x2F;&#x2F;books.google.com&#x2F;books&#x2F;about&#x2F;The_Haskell_School_of_E...</a> on it.
pgtover 4 years ago
Functional Composition by Chris Ford: <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=Mfsnlbd-4xQ" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=Mfsnlbd-4xQ</a>
whiddershinsover 4 years ago
Very interesting article.<p>However, this is false:<p>“ that’s why CD music had a sample rate of 22000 Hz. Modern sound cards however tend to use sampling rates twice as high - 44100 Hz or 48000 Hz”<p>CDs are&#x2F;were 44100 hz, they were never 22000, because you need two samples per wave and because you need slight over sampling and to make the conversion from 48k in to bad math to discourage DAT machine as a lossless copy format for the masses.
评论 #24951407 未加载
评论 #24951352 未加载
dcanelhasover 4 years ago
I thought CDs had a sampling rate of 44kHz? You need twice as many samples as the frequency you want to represent, after all (to make a wave you need both a positive anda negative sample in the same period). This is the essence of the Nyquist-Shannon a.k.a. the sampling theorem.
评论 #24949947 未加载
devilduckover 4 years ago
That&#x27;s neat, but doing sound design and creating listenable&#x2F;good compositions are two different endeavors. You&#x27;d likely be better off writing an actual DSP engine in C than trying to diddle with frequencies in this way. Use Supercollider if you actually want to make music with code rather than just do little experiments like this.
amboo7over 4 years ago
I wrote this trying to compress the notes: <a href="https:&#x2F;&#x2F;github.com&#x2F;amb007&#x2F;sound-series" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;amb007&#x2F;sound-series</a>
calmchaosover 4 years ago
Great stuff! Now who&#x27;s the first person to listen to the entire linux kernel source code? ;)
thecodrrover 4 years ago
Hyped to see someone implement this in C#. This is great!
vezycashover 4 years ago
Is there any hum to sound software?
tobyhinloopenover 4 years ago
Cds were 44100Hz, right?
yowlingcatover 4 years ago
Obligatory plug to TidalCycles [1], which is a Haskell based system folks have begun to use for live coding with IMO impressive results.<p>[1] <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=sNj-I2pZwX8" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=sNj-I2pZwX8</a>