Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7189285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:16:12+00:00 2026-05-28T19:16:12+00:00

I’m trying to make a (super) simple software instrument for a new project I’m

  • 0

I’m trying to make a (super) simple software instrument for a new project I’m working on. The way I was going about doing this is similar to Garageband’s “musical typing”, where certain notes are bound to certain keys on your keyboard. What I’ve been attempting to is bind super small mp3’s (~100kb each) to buttons and keys. The way I did this is as follows:

    var a = document.createElement('audio');
    var s = document.createElement('audio');
    var d = document.createElement('audio');
    var f = document.createElement('audio');

    a.setAttribute('src', 'Sounds/a.mp3');

    $('.a').click(function() {
       a.currentTime = 0;
       a.play();
    });

    $(document).jkey('a',function(){
       a.currentTime = 0;
       a.play();
       return false;
    });

For the key binding, I am using the jKey jquery plugin.

Things work (mostly) but there are a few problems:

  1. In Chrome, when the key or button is pressed, the sound skips and then seems to restart again real fast. Firefox doesn’t work (firebug says it’s something to do with the .currentTime), but Safari works perfectly.

  2. When holding the keydown, I would like it to NOT keep executing the function. Currently, holding the key down keeps replaying the bit, resulting in a “dun dun dun dun dun dun dun dun dun dun dun” sound.

Any suggestions on how to fix the following is much appreciated. I tried the regular ways to bind keys in Javascript and I am getting the same results, so I know it’s not jKey.

Also – if anyone has any suggestions how to go about doing this an entirely different / better way, feel free to tell me! Thanks!

I should also mention that the .current time is so that it starts replaying the note as soon as the button is pressed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:16:12+00:00Added an answer on May 28, 2026 at 7:16 pm

    I’m not familiar with jKey, but I believe most browsers do generate multiple keydown events if a key is held down so I guess that explains the “dun dun dun dun” sound you mention. In a general sense if you want to do something exactly once when a key is pressed I think you’ll need some sort of flag that you reset on keyup. I’d suggest something like this to keep things tidy:

    var keyNotes = {
           addNote : function(key, audioSrc) {
               var el = document.createElement('audio');
               el.setAttribute('src', audioSrc);
               // any other element init here, presumably you
               // append it to some container, then:
    
               this[key] = {
                   audio   : el,
                   canPlay : true
               };
           },
           play : function(key) {
               var note = this[key];
               // if note has been defined for this key, and can be played
               if (note && note.canPlay) {
                   note.canPlay = false;
                   note.audio.currentTime = 0;
                   note.audio.play();
               }
           },
           stop : function(key) {
               var note = this[key];
               if (note && !note.canPlay) {
                  note.canPlay = true;
                  note.audio.pause(); // or should this be .stop()?
               }
           }
        };
    
    keyNotes.addNote("a", "Sounds/a.mp3");
    keyNotes.addNote("s", "Sounds/b.mp3");
    keyNotes.addNote("d", "Sounds/c.mp3");
    keyNotes.addNote("f", "Sounds/d.mp3");
    
    $(document).keydown(function(e){
        keyNotes.play(noteFromKeyCode(e.which));
        return false;
    }).keyup(function(e){
        keyNotes.stop(noteFromKeyCode(e.which));
    });
    
    function noteFromKeyCode(keyCode) {
        return String.fromCharCode(keyCode).toLowerCase();
    }
    

    (You’ll need to check that for syntax errors, but I hope the general idea is obvious.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.