r/dailyprogrammer • u/G33kDude 1 1 • Jun 15 '16
[2016-06-15] Challenge #271 [Intermediate] Making Waves
This challenge is a bit uncoventional, so I apologize in advance to anyone who may feel excluded due to language or other constraints. Also, I couldn't think of fun backstory so feel free to make one up in your comments.
Description
For today's challenge we will be focusing on generating a serieses waveforms at specific frequencies, known as musical notes. Ideally you would be able to push these frequencies directly to your speakers, but this can be difficult depending on your operating system.
For Linux systems with ALSA, you can use the aplay utility.
./solution | aplay -f U8 -r 8000For other systems you can use Audacity, which features a raw data import utility.
Input Description
You will be given a sample rate in Hz (bytes per second), followed by a duration for each note (milliseconds), and then finally a string of notes represented as the letters A through G (and _ for rest).
Output Description
You should output a string of bytes (unsigned 8 bit integers) either as a binary stream, or to a binary file. These bytes should represent the waveforms[1] for the frequencies[2] of the notes.
Challenge Input
8000
300
ABCDEFG_GFEDCBAChallenge Output
Since the output will be a string of 36000 bytes, it is provided below as a download. Note that it does not have to output exactly these bytes, but it must be the same notes when played.
You can listen to the data either by playing it straight with aplay, which should pick up on the format automatically, or by piping to aplay and specifying the format, or by importing into audacity and playing from there.
Bonus
Wrap your output with valid WAV/WAVE file headers[3] so it can be played directly using any standard audio player.
Notes
- Wikipedia has some formulas for waveform generation. Note that
tis measured in wavelengths. - This page lists the exact frequencies for every note.
- A good resource for WAV/WAVE file headers can be found here. Note that by "Format chunk marker. Includes trailing null", the author of that page means trailling space.
- One of our readers pointed out that to accurately (re)construct a given audio signal via discrete samples, the sampling rate must (strictly) exceed twice the highest frequency from that signal. Otherwise, there will be artifacts such as 'aliasing'. Keep this in mind when experimenting with higher octaves, such as the 8th and above.
Finally
Have a good challenge idea?
Consider submitting it to /r/dailyprogrammer_ideas
8
u/G33kDude 1 1 Jun 15 '16 edited Jun 16 '16
Solution in Python3. Implements bonus, but it does not implement input parsing.
Audacity Screenshots:
Technical Details:
GitHub Link
3
u/bearific Jun 15 '16 edited Jun 15 '16
Python 3 Implements bonus using the wave module to create a .wav file and the winsound module to play the file on windows.
Result: wav file
Also tried an old nursery rhyme.
3
u/Starbeamrainbowlabs Jun 15 '16
Umm I have no idea how to output raw audio in any language. Does anyone have a guide or tutorial I can follow?
It might be helpful for others in the future if a link or two is added to this challenge's description.
3
u/weekendblues Jun 16 '16
Java
Writes a signed 16bit wav to stdout. I may extend it to accept more notes and wave types. Took me way longer than I'd like to admit to realize I was having problems because I needed to use "fmt " rather than "fmt\0".
Example:
yields this wav.
2
u/mbdomecq Jun 16 '16 edited Jun 16 '16
This took me way too long but I think I got it. Outputs text in the format of a .WAV file; as far as I know it works for every valid input and is platform-independent but it's also possible that it only works on Windows :/
C++
Edit:
A couple of other fun inputs:
44100 100 E_____E_G__E__C_B_______A_______44100 75 E_E___E___C_E___G_______1
u/G33kDude 1 1 Jun 15 '16 edited Jun 15 '16
Solution in AutoHotkey. Implements bonus, does not implement input parsing (it allows for far more variables than in the described input, so it'd be a little odd to split up the configuration).
Output of the second two amplitude options: https://i.imgur.com/FN8Hq1U.png
4
u/Godspiral 3 3 Jun 15 '16 edited Jun 15 '16
in J6.02,
with a bpm parameter,
lhs is durations (reciprocals). rhs are frequencies, where 0 is note C
1
u/Scroph 0 0 Jun 16 '16 edited Jun 16 '16
C solution with bonus.
It took me a while to get the pcm > wav conversion to work. After I spent a great amount of time debugging it I ended up narrowing the problem down to the absence of a space in "fmt ", I had it write "fmt\0" instead.
The output is noisy though, like my computer has a cold or something.
2
u/reddit_account_30 Jun 15 '16
Matlab. Challenge input is ran with noteGen(8000,300, 'ABCDEFG_GFEDCBA').
2
u/ShaharNJIT 0 1 Jun 15 '16 edited Jun 15 '16
JavaScript Download is messed up sometimes so don't know if bonus counts. Works fine with a standard sample rate (i.e. 44100 Hz). The code I have for the download is from Recorderjs. Fiddle: https://jsfiddle.net/w9ck21h3/
2
u/adrian17 1 4 Jun 15 '16
Simple C, no invalid input checks.
Usage on Linux:
2
u/netbpa Jun 15 '16
Perl6 solution, no bonus
2
u/FelixMaxwell 1 0 Jun 15 '16
Python 3
Outputs to pcm file. First argument is the input file, second argument is the output.
2
u/[deleted] Jun 16 '16
C++ , not very font of my solution and it took me way too long for what it is.
I didn't bother adding the .wav part either. It does also handle input of the octaves and #/b notes, although to implement this more easily I took the liberty of separating the notes with commas.
CommandLine:
Output: https://my.mixtape.moe/txkowr.wav.
2
u/noofbiz Jun 16 '16
C# https://gist.github.com/Noofbiz/58410ebadd7971ebc57f62d63ff8f0d2
2
u/Pawah Jun 16 '16
MATLAB solution:
You can run it by calling y = making_waves(8000, 300, 'ABCDEFG_GFEDCBA'). y would be the sound waveform, which you can save to a wav file or play it with the sound command
2
u/Thelta Jun 16 '16 edited Jun 16 '16
After working on it for hours, i finished it.Solution in C, it takes input as parameters, output is two files named answer.pcm and answer.wav.As you can guess i also did bonus.
2
u/voice-of-hermes Jun 17 '16 edited Jun 17 '16
Bonus implemented on the command line, because pipeline processing kicks ass:
Code:
2
u/rakkar16 Jun 17 '16
Python 3
I think in the end most of the work went into the bonus. I decided not use use pre-existing libraries to make a wave file, to stay in the spirit of the challenge.
2
u/FrankRuben27 0 1 Jun 18 '16
late to the party, but first solution in Ocaml, incl WAV output:
For raw output:
For wav output:
2
u/nibbl Jun 22 '16
My first time here and I just want to say thank you - that was really interesting assignment :)
Python3.5:
2
u/sdlambert Jun 30 '16 edited Jun 30 '16
Solution in Javascript (Node)~~~~
The output is slightly scratchy on the A but not any other frequencies and I have no idea why.My upper bounds were going past 255, sounds great now
3
u/wizao 1 0 Jun 15 '16
Haskell:
I was reading the Haskell School of Music to learn Euterpea for this challenge. I only had to time get the music to play instead of writing to a wav file. I'm only halfway through the tutorial, and the other half is has utilities to convert
Music ato wav files. I will try to update when I get the chance.1
u/OmniSable Jun 16 '16
Decided to do the bonus only. Python3.4 with wave. Outputs the WAV file.
https://gist.github.com/Highstaker/09913478cdb10977272a115586d3ba2a#file-2016-06-15-challenge-271-intermediate-making-waves-py