The sound of the theremin has been immortalized in The Beach Boys song Good Vibrations. Many also associate its sound with the theme for the original series of Star Trek, though apparently it was a soprano's emulation.
This challenge requires you to implement a theremin.
- Take 2 dimensional input from a mouse or other input device (e.g. you could use a joystick), which will produce a tone from an audio output device as follows:
- increasing x will increase the frequency of the tone. Note that frequency increases exponentially with musical note, so you must implement a linear relationship between mouse x position and the musical note, and
- increasing y will increase the volume of the tone.
- There appears to be confusion regarding the waveform produced by a real theremin, so for simplicity, a sine wave (or close approximation thereof) must be used.
- The tone produced must have at least a 2-octave range. More range is acceptable. A-440 must lie within the range.
- In order to create the audible appearance of continuously variable frequency and amplitude, the range of values considered in both dimensions from the input device must be at least 500 Implementations may open a window (at least 500x500 pixels) to read input from mouse cursor position. Or without opening a window, coordinates may be read directly from the mouse or other input device.
- There must be a simple means to stop the program - key-combination, mouse-click or other common input device. CTRL-c is sufficient.
- Any standard libraries may be used, as long as they do not totally implement the solution with no other work required.
- Standard rules for Code Golf and I/O
- You may stop by having the mouse or input device lose focus on the input box.
- Because the output tone is dynamically-generated, care must be take to ensure the tone is a continuous waveform; that is there are no audible clicks or pops caused by sudden changes of phase or amplitude.
- You may limit the input size zone to 500 by 500 but it may be bigger.
This is code-golf, so the shortest answer in any language wins.
Special thanks to Digital Trauma for making this challenge and posting it in the Secret Santa's Sandbox. I have made a few edits and here is the original post.