Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="container">
  <h1>Retro Ringtones</h1>
  <textarea rows="1" id="melody" type="textArea" placeholder="2#a1 2f1 4- 8#a1 8c2 8d2 8#d2 2f2 2- 4f2 "></textarea>
<button onclick=run() class="green" id="run"> play </button>
  
  <div class="controlls">
      <label for="volume">Volume:</label>
      <input type="range" min="0.01" max="1" value=".5" step="0.01" class="slider"  id="volumeRange" oninput="updateVolume()"> <p id="volumeDisplay">50%</p>
    <label for="volume">BPM:</label>
      <input type="range" min="10" max="300" value="100" step="1" class="slider"  id="bpmRange" oninput="updateBPM()"> <p id="bpmDisplay"> 100bpm </p>
  </div>
  <p> Enter notes in the textbox above and hit play to hear them in all there monotone glory. use the buttons below to autopopulate some samples.</p>
  <div class="songs">
      <button class="song" onclick=populate("comefaithfull")>Oh Come All Ye Faithful</button>
   <button class="song" onclick=populate("zelda")>The legend of zelda</button>
     <button class="song" onclick=populate("indiana")>Indiana Jones</button>
       <button class="song" onclick=populate("adams")>The Adams Family</button>
       <button class="song" onclick=populate("simpsons")>The Simpsons</button>
       <button class="song" onclick=populate("rickRoll")>Never gonna give you up</button>
    <button class="song" onclick=populate("titanic")>titanic</button>
    
    
  </div>
</div>
              
            
!

CSS

              
                * {
	box-sizing: border-box;
   font-family: 'VT323', monospace;
}

body {
  background-color:#eaeaea;
  width:100%;
}

h1{
  font-weight:bold;
  font-size:3rem;
}

.container {
  display:flex;
  align-items: center;
  justify-content: center;
  flex-direction:column;
}

#melody{
  outline:none;
  background:transparent;
  box-shadow:none;
  border:none;
  border-bottom: rgba(200,0,0,0.3) 2px solid;
  overflow: hidden;
  width:600px;
  font-weight:bold;
  font-size:2rem;
  text-align: center;
  color:rgba(130,20,20,0.7);
}

@mixin block($col: #881400,$highlight:#A81000,$shadow:6px) {
  box-shadow:none;
  border:none;
	border-bottom: $shadow inset rgba(0,0,0,.5);
	border-left: $shadow inset rgba(0,0,0,.5);
	border-right: $shadow inset rgba(255,255,255,.5);
	border-top: $shadow inset rgba(255,255,255,.5);
	box-sizing: border-box;
  color: white;
	cursor: pointer;
  font-size: 1rem;
  letter-spacing: 2px;
	margin: 1rem;
	padding: .25rem;
	text-transform: uppercase;
	width: auto;
   background:$col;
   user-select: none;
  	&:focus,
	  &:hover {
		background: $highlight;
	}
}

button{
  @include block;
  &.green {
    @include block(#005800,#006800);
  }
  
  &.song{
    @include block("","",3px);
    margin:0;
    margin-right:.2rem;
    font-size: .75rem;
    letter-spacing: 2px;
  }
}

.controlls  {
  > *{
  display:inline-block;
  font-weight:500;
  font-size:1.2rem;
  }
  
  p{
    margin-right:3.5rem;
    font-weight:700;
  }
}


.slider {
    appearance: none;
    width: 200px;
    height: 5px;
    background: #d3d3d3; 
    outline: none; 
    opacity: 0.7; 
  
}

.slider::-webkit-slider-thumb {
     @include block(black,gray);
    appearance: none;
    width: 15px; 
    height: 15px; 
  transition: background-color .2s;

}

.songs{
  text-align:center;
}
              
            
!

JS

              
                var notes = {
    'C0':16.35,
   'C#0': 17.32,
   '#C0': 17.32,
   'Db0': 17.32,
   'D0': 18.35,
   'D#0': 19.45,
   '#D0': 19.45,
   'Eb0': 19.45,
   'E0': 20.60,
   'F0': 21.83,
   'F#0': 23.12,
   '#F0': 23.12,
   'Gb0': 23.12,
   'G0': 24.50,
   'G#0': 25.96,
   '#G0': 25.96,
   'Ab0': 25.96,
   'A0': 27.50,
   'A#0': 29.14,
   '#A0': 29.14,
   'Bb0': 29.14,
   'B0': 30.87,
   'C1': 32.70,
   'C#1': 34.65,
   '#C1': 34.65,
   'Db1': 34.65,
   'D1': 36.71,
   'D#1': 38.89,
   '#D1': 38.89,
   'Eb1': 38.89,
   'E1': 41.20,
   'F1': 43.65,
   'F#1': 46.25,
   '#F1': 46.25,
   'Gb1': 46.25,
   'G1': 49.00,
   'G#1': 51.91,
   '#G1': 51.91,
   'Ab1': 51.91,
   'A1': 55.00,
   'A#1': 58.27,
   '#A1': 58.27,
   'Bb1': 58.27,
   'B1': 61.74,
   'C2': 65.41,
   'C#2': 69.30,
   '#C2': 69.30,
   'Db2': 69.30,
   'D2': 73.42,
   'D#2': 77.78,
   '#D2': 77.78,
   'Eb2': 77.78,
   'E2': 82.41,
   'F2': 87.31,
   'F#2': 92.50,
   '#F2': 92.50,
   'Gb2': 92.50,
   'G2': 98.00,
   'G#2': 103.83,
   '#G2': 103.83,
   'Ab2': 103.83,
   'A2': 110.00,
   'A#2': 116.54,
   '#A2': 116.54,
   'Bb2': 116.54,
   'B2': 123.47,
   'C3': 130.81,
   'C#3': 138.59,
   '#C3': 138.59,
   'Db3': 138.59,
   'D3': 146.83,
   'D#3': 155.56,
   '#D3': 155.56,
   'Eb3': 155.56,
   'E3': 164.81,
   'F3': 174.61,
   'F#3': 185.00,
   '#F3': 185.00,
   'Gb3': 185.00,
   'G3': 196.00,
   'G#3': 207.65,
   '#G3': 207.65,
   'Ab3': 207.65,
   'A3': 220.00,
   'A#3': 233.08,
   '#A3': 233.08,
   'Bb3': 233.08,
   'B3': 246.94,
   'C4': 261.63,
   'C#4': 277.18,
   '#C4': 277.18,
   'Db4': 277.18,
   'D4': 293.66,
   'D#4': 311.13,
   '#D4': 311.13,
   'Eb4': 311.13,
   'E4': 329.63,
   'F4': 349.23,
   'F#4': 369.99,
   '#F4': 369.99,
   'Gb4': 369.99,
   'G4': 392.00,
   'G#4': 415.30,
   '#G4': 415.30,
   'Ab4': 415.30,
   'A4': 440.00,
   'A#4': 466.16,
   '#A4': 466.16,
   'Bb4': 466.16,
   'B4': 493.88,
   'C5': 523.25,
   'C#5': 554.37,
   '#C5': 554.37,
   'Db5': 554.37,
   'D5': 587.33,
   'D#5': 622.25,
   '#D5': 622.25,
   'Eb5': 622.25,
   'E5': 659.26,
   'F5': 698.46,
   'F#5': 739.99,
   '#F5': 739.99,
   'Gb5': 739.99,
   'G5': 783.99,
   'G#5': 830.61,
   '#G5': 830.61,
   'Ab5': 830.61,
   'A5': 880.00,
   'A#5': 932.33,
   '#A5': 932.33,
   'Bb5': 932.33,
   'B5': 987.77,
   'C6': 1046.50,
   'C#6': 1108.73,
   '#C6': 1108.73,
   'Db6': 1108.73,
   'D6': 1174.66,
   'D#6': 1244.51,
   '#D6': 1244.51,
   'Eb6': 1244.51,
   'E6': 1318.51,
   'F6': 1396.91,
   'F#6': 1479.98,
   '#F6': 1479.98,
   'Gb6': 1479.98,
   'G6': 1567.98,
   'G#6': 1661.22,
   '#G6': 1661.22,
   'Ab6': 1661.22,
   'A6': 1760.00,
   'A#6': 1864.66,
   '#A6': 1864.66,
   'Bb6': 1864.66,
   'B6': 1975.53,
   'C7': 2093.00,
   'C#7': 2217.46,
   '#C7': 2217.46,
   'Db7': 2217.46,
   'D7': 2349.32,
   'D#7': 2489.02,
   '#D7': 2489.02,
   'Eb7': 2489.02,
   'E7': 2637.02,
   'F7': 2793.83,
   'F#7': 2959.96,
   '#F7': 2959.96,
   'Gb7': 2959.96,
   'G7': 3135.96,
   'G#7': 3322.44,
   '#G7': 3322.44,
   'Ab7': 3322.44,
   'A7': 3520.00,
   'A#7': 3729.31,
   '#A7': 3729.31,
   'Bb7': 3729.31,
   'B7': 3951.07,
   'C8': 4186.01
}
let songs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c2 2d2 4#c2 4#c2 2d2 4e2 2#f2 2e2 1d2`,
  comefaithfull:`4g1 2g1 4d1 4g1 2a1 2d1 4b1 4a1 4b1 4c2 2b1 4a1 4g1 2g1 4#f1 4e1 4#f1 4g1 4a1 4b1 2#f1 4.e1 8d1 2.d1`
}

let playing = false;
let osc = null;
let nextNoteTimeout =null;
let melody = []
let index = 0;
let bpm = 100
let baseLength = 1000;
//http://nokia.nigelcoldwell.co.uk/tunes.html

var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var oscillator = audioCtx.createOscillator();
let gainNode = audioCtx.createGain();
gainNode.gain.value = 0.5;

function playNote(){
  
  if(index < melody.length) {
    let melodyPos = melody[index];
    let noteLength =/^[0-9]*/.exec(melodyPos)[0];
    let curNote = notes[melodyPos.substring(noteLength.length)] || 0;
    if(!noteLength) noteLength = 1;
    //console.log(`playing note: ${melody[index]} (${curNote}) for ${noteLength}`)
    osc.frequency.setValueAtTime(curNote, audioCtx.currentTime); 
    nextNoteTimeout = setTimeout(playNote,baseLength/noteLength);
    index++;
  }
  else{
    pause();
  }
}

let run = function() {
  if(playing) pause();
  else play();
}

let pause = function() {
  index =0;
  clearTimeout(nextNoteTimeout);
  document.getElementById("run").innerHTML = "play";
  playing = false;
  osc.stop();
}

let play = function() {
  playing= true;
   document.getElementById("run").innerHTML = "pause";
    melody = document.getElementById("melody").value.toUpperCase().split(" ");
    osc = audioCtx.createOscillator();
    osc.type = 'square';
    osc.frequency.setValueAtTime(0, audioCtx.currentTime); 
    osc.connect(gainNode);
    gainNode.connect(audioCtx.destination)
    osc.start();
    index =0;
    playNote();
}

let updateVolume =function() {
 gainNode.gain.value = document.getElementById("volumeRange").value;
  document.getElementById("volumeDisplay").innerHTML = `${Math.round(gainNode.gain.value*100)}%`
}

let updateBPM = function() {
  bpm = document.getElementById("bpmRange").value;
  //at 100bpm every 60 seconds we want 100 beats
  //(60/100) *1000
  baseLength = (60/bpm) * 1000;
  document.getElementById("bpmDisplay").innerHTML = `${bpm}bpm`
}

let populate = function(song) {
  document.getElementById("melody").value = songs[song]
}
              
            
!
999px

Console