Building a Metronome

36

3

Introduction

Some days ago I needed a metronome for something. I had none available so I downloaded an app from the App Store. The app had a size of 71 MB!!!
71 MB for making tic-toc...?!
So code-golf came into my mind and I was wondering if some of you guys could improve this.

Challenge

Golf some code that outputs some sound. It's pretty irrelevant what kind of sound. If required create some sound file... but a System beep will do the job as well. (Here is some sound I created... nothing special.)

Input: The beats per minute the metronome outputs.

Example

This is a non-golfed Java-version! It's just to show you the task.

public class Metronome {
  public static void main(String[] args) throws InterruptedException {
    int bpm = Integer.valueOf(args[0]);
    int interval = 60000 / bpm;

    while(true) {
        java.awt.Toolkit.getDefaultToolkit().beep();
        // or start playing the sound
        Thread.sleep(interval);
        System.out.println("Beep!");

    }
  }
}

Rules

You may not use external libaries, only tools of the language itself are allowed.
Only the bytes of the source code count... not the sound file.

This is , so the submission with the least amount of bytes wins!

EDIT:

Example output: So something like this would be the output for 120 bps: link

PEAR

Posted 2016-01-23T13:59:36.777

Reputation: 469

1Can you add a few examples for I/O (record some sound and upload it, post the links here)? – Addison Crump – 2016-01-23T14:25:53.780

What kind of I/O example do you mean? P.S. There is already some sound I created, look under Challange ;) – PEAR – 2016-01-23T14:28:47.553

Oh, I know about that. It's just that we typically show a few input/output examples. – Addison Crump – 2016-01-23T14:29:51.980

Oh, ok... I'll edit the question. – PEAR – 2016-01-23T14:31:12.317

2Question: when you say "external libraries", does that include the libraries that are suggested with the language? (I won't use this, but an example is in Vitsy wherein I can access shell or JS (but JS is builtin)) – Addison Crump – 2016-01-23T17:48:15.597

Is the answer allowed to run out of memory or blow the call stack at some point? – Martin Ender – 2016-01-23T18:33:01.993

Are command line options for an interpreter counted / allowed? – ChatterOne – 2016-01-23T18:43:30.767

Can we use external audio sounds? – Downgoat – 2016-01-23T18:47:20.763

@DOWNGOAT: Of course you can! You can use the file I uploaded (see question) or use another sound. This is not important. – PEAR – 2016-01-23T18:51:30.987

@ChatterOne Yes. Count how many bytes you need to add to the invocation which is usually between 1 and 3 bytes for single-character flags.

– Martin Ender – 2016-01-23T18:52:00.963

@ETHproductions: Yes, you can. But it's even shorter if you download and rename them, isn't it? – PEAR – 2016-01-23T18:55:30.593

Can we get the input speed from a file? – Downgoat – 2016-01-23T19:06:40.230

@Doᴡɴɢᴏᴀᴛ: Yes, I did not specify the input. – PEAR – 2016-01-23T19:08:19.470

3

Can you add a leaderboard snippet in?

– Addison Crump – 2016-01-23T21:53:08.400

1I suspect the majority of that app you downloaded is pretty graphics and sound effects. It's like those flashlight apps that do nothing but turn the screen all white but still manage to somehow use up tens of MB... – Darrel Hoffman – 2016-01-24T15:13:48.380

1What's the requirement on accuracy? In your sample, both beep() and console output aren't exactly instant IIRC. Neither sleep() is known for beeing accurate. – Num Lock – 2016-01-25T09:04:49.290

Answers

19

Mathematica, 26 bytes

Pause[Beep[];60/#]~Do~∞&

Do is normally used as a "for" loop in the narrowest sense: repeat this piece of code for each i from x to y... or even just repeat this piece of code n times. Instead of a number n we can give it infinity though to create an infinite loop. The loop body is Pause[Beep[];60/#] which is just a golfy way of writing Beep[];Pause[60/#] where # is the function argument.

If it's admissible for the solution to blow up the call stack eventually, we can save one byte with a recursive solution:

#0[Beep[];Pause[60/#];#]&

Martin Ender

Posted 2016-01-23T13:59:36.777

Reputation: 184 808

I didn't know that ~Do~∞ was possible. A For loop only got me to 29 bytes. (Also, I personally believe that the 26-byte version is the only valid one.) – LegionMammal978 – 2016-01-23T18:51:01.697

@LegionMammal978 Unfortunately, ~Do~∞ doesn't seem to work when the comes from a variable. (I tried using that when golfing your truth machine.) – Martin Ender – 2016-01-23T18:52:44.077

1Attributes[Do] includes HoldAll, so my guess is that _~Do~∞ has a special evaluation pattern. – LegionMammal978 – 2016-01-23T19:00:11.787

@LegionMammal978 It seems more like variables do, because the error message for Do[...,a] where a holds infinity actually shows the call as Do[...,{a}]. – Martin Ender – 2016-01-23T19:01:06.863

14

Pyth, 11 10 9 bytes

Thanks to Adnan for reminding me about #.

#C7.dc60Q

Forever (#), print Char code 7. Then sleep (.d) 60 seconds divided by (c) input (Q).

PurkkaKoodari

Posted 2016-01-23T13:59:36.777

Reputation: 16 699

@Adnan Forgot about that one. Thanks. – PurkkaKoodari – 2016-01-23T18:26:38.383

Do you need the space? – lirtosiast – 2016-01-23T18:30:23.677

@ThomasKwa Yes. IIRC 7. would be parsed as a number. – Conor O'Brien – 2016-01-23T18:30:49.570

@CᴏɴᴏʀO'Bʀɪᴇɴ The problem is that Pyth would try to print the None returned by .d as .d is also the "get date" function. (If spamming empty lines to the console is allowed, then the space is not needed.) – PurkkaKoodari – 2016-01-23T18:34:36.480

5Oh. #pythnoob – Conor O'Brien – 2016-01-23T18:35:40.650

@CᴏɴᴏʀO'Bʀɪᴇɴ Hold on. Apparently Nones are not implicitly printed. Profit – PurkkaKoodari – 2016-01-23T18:37:31.843

@Pietu1998 I thought of this sort of thing, actually. – isaacg – 2016-01-23T22:49:55.033

2I couldn't get .d to sleep when i tried. It kept printing unix time – busukxuan – 2016-01-24T17:50:06.223

@busukxuan Are you using the latest version of Pyth? Try doing git pull in the installation directory. Sleeping was added somewhat recently. (Also, UNIX timestamp is .d0 which should never happen with this code.) – PurkkaKoodari – 2016-01-24T18:02:15.477

I actually downloaded it 5days ago – busukxuan – 2016-01-24T18:04:57.667

8

JavaScript, 36 45 42 41 34 bytes

Saved 1 byte thanks to @RikerW

Saved 1 byte thanks to @ETHproductions

n=>{for(;;sleep(60/n))print("\7")}

This is a function.

If I use `\7`, SpiderMonkey complains octal literals are deprecated.

Alternative, 31 bytes

n=>{for(;;sleep(60/n))print``}

The problem is the unprintables are stripped but this should work.

Downgoat

Posted 2016-01-23T13:59:36.777

Reputation: 27 116

Dammit, I was just about to post something like this. I'm still going to post it (because it uses node and all) because I use a different approach. – Addison Crump – 2016-01-23T18:18:23.580

If you look it from the way I asked the question, the recursive solution would not be possible. Metronomes are made for working and working... not for crashing after some time. – PEAR – 2016-01-23T19:12:41.787

@PEAR this shouldn't crash because no variable is being increments. The only thing that might cause it to crash is the terminal buffer except on modern computers that could take > 50-100 years I think – Downgoat – 2016-01-23T19:16:45.020

What environment does this run under? I've tried chrome and Node.js, but I can't get it to work. – starbeamrainbowlabs – 2016-04-13T17:07:32.100

@starbeamrainbowlabs this uses the JavaScript shell (SpiderMonkey) – Downgoat – 2016-04-13T22:02:52.887

You mean in firefox? – starbeamrainbowlabs – 2016-04-14T17:50:03.713

@starbeamrainbowlabs no, the SpiderMonkey shell.

– Downgoat – 2016-04-14T22:50:54.713

Oh, right! I've never heard of it. I'll have to check it out! – starbeamrainbowlabs – 2016-04-15T16:33:17.123

On Chrome, this pulls up the prompt to print a hardcopy of the webpage – Patrick Roberts – 2017-02-05T12:29:22.153

8

Bash, 53 55 41 bytes

Thanks to @Dennis for shaving off 14 bytes1

Okay, truth time: I'm terrible at golfing bash. Any help would be so very appreciated.

echo " ";sleep `bc -l<<<60/$1`;exec $0 $1
      ^ That's ASCII char 7

1 Holy crap. No wonder nobody can outgolf Dennis.

Addison Crump

Posted 2016-01-23T13:59:36.777

Reputation: 10 763

Is while 1 possible? – PEAR – 2016-01-23T19:02:50.260

@PEAR Nupe - already tried that. – Addison Crump – 2016-01-23T19:03:57.583

while printf \\a perhaps? – Neil – 2016-01-23T20:19:18.603

This doesn't work since bash uses integer division. You'll need to use bc. – a spaghetto – 2016-01-23T20:37:20.160

>

  • The BEL character isn't special to Bash, so you don't need the quotes. 2. If you read the input as a CLA, you don't need read. 3. echo exists with code 0, so you can use that statement instead of true.
  • < – Dennis – 2016-01-24T02:11:32.270

    Even better: Use exec $0 $1 instead of the while loop. – Dennis – 2016-01-24T02:34:57.517

    7

    JavaScript ES6 (browser), 43 bytes

    This may be stretching the rules:

    x=>setInterval('new Audio(1).play()',6e4/x)
    

    Give this function a name (e.g. F=x=>...) and enter it in the browser console on this page. Then call the function with your bps, e.g. F(60), and wait for the magic to happen. :-)

    Why does this work? Well, b.html is in the same folder as a file named 1, which is the sample sound file from the OP. I'm not sure if this is within the rules (I guess it's like the shell version; it needs to be run in a specific environment), but it was worth a shot.

    Safer version, 57 bytes

    If the above code isn't allowed for some reason, try this instead:

    x=>setInterval('new Audio("//ow.ly/Xrnl1").play()',6e4/x)
    

    Works on any page!

    ETHproductions

    Posted 2016-01-23T13:59:36.777

    Reputation: 47 880

    This is an intersting solution. It's even shorter when you download and rename the file, isn't it? – PEAR – 2016-01-23T18:57:06.580

    @PEAR That would be shorter, but then it would need its own webpage with the sound file in the same folder to run. – ETHproductions – 2016-01-23T18:57:45.647

    Oh, it's JavaScript xD... you're right – PEAR – 2016-01-23T19:10:00.510

    @PEAR There, I did it. Is this new solution within the rules? – ETHproductions – 2016-01-23T19:10:52.360

    Huh. You could specify that it is JS with the certain webpage. It's a preexisting interpreter, so it's a valid language. – Addison Crump – 2016-01-23T19:11:15.337

    Hey, it works :) – PEAR – 2016-01-23T19:25:05.467

    6

    Python, 68 67 57 bytes

    Saved 1 byte thanks to @FlagAsSpam

    Saved 9 bytes thanks to @Adnan

    import time
    a=input()
    while 1:print"\7";time.sleep(60./a)
    

    Also it took 2 bytes less after converting line endings to UNIX format.

    Older version, that actually takes bpm as command line argument (66 bytes):

    import sys,time
    while 1:print"\7";time.sleep(60./int(sys.argv[1]))
    

    webwarrior

    Posted 2016-01-23T13:59:36.777

    Reputation: 101

    4Can't you do print"\7";? I'm not sure, but I'm pretty sure that works. – Addison Crump – 2016-01-23T18:16:20.723

    @Andan No, input() requests input from user. I don't know if that's considered a valid input. Also conversion to number is needed anyway. – webwarrior – 2016-01-23T21:19:26.920

    1How about a=input() and a replacing int(sys.argv[1])? I've always thought that Python 2 automatically evaluates input and therefore doesn't need the int conversion, but I may be wrong. – Adnan – 2016-01-24T11:45:16.160

    @Andan input() actually does auto evaluate. I forgot about that feature. It's rather unpythonic though - probably a legacy from old times. – webwarrior – 2016-01-24T15:15:40.493

    Can time.sleep(60./a) be replaced with time.sleep(60./input()), while completely removing a=input()? – clap – 2016-01-25T04:47:18.570

    @VoteToSpam No, that would ask user for input on every iteration – webwarrior – 2016-01-25T13:50:30.003

    @webwarrior Oh... yeah... I can code... – clap – 2016-01-25T17:41:56.597

    You can save another byte by literally using the BEL character instead of \7 in your code. – Alexander Revo – 2016-01-29T10:19:20.950

    6

    05AB1E, 31 bytes

    Code:

    I60s/[7ç?D.etime.sleep(#.pop())
    

    If I had a built-in for waiting N seconds, this could have been 11 bytes. Unfortunately, this is not the case. Here is the explanation:

    I                               # Push input
     60                             # Push 60
       s                            # Swap the top 2 items
        /                           # Divide the top 2 items
         [                          # Infinite loop
          7ç                        # Push the character \x07
            ?                       # Output it, which give a sound
             .e                     # Evaluate the following as Python code
               time.sleep(       )  # Wait for N seconds
                          #         # Short for stack
                           .pop()   # Pop the last item
    

    Uses the ISO 8859-1 encoding.

    Adnan

    Posted 2016-01-23T13:59:36.777

    Reputation: 41 965

    This must be one of the first 05AB1E answers o.Ô It looks very weird to see the time.sleep and .pop() in the middle of the code like that. ;) – Kevin Cruijssen – 2019-08-20T16:54:34.440

    6

    osascript, 39 bytes

    on run a
    repeat
    beep
    delay 60/a
    end
    end

    There is literally a command called beep? Sweeeet!

    Runnable only on Mac OS X due to restricted license, but to run, do:

    osascript -e "on run a
    repeat
    beep
    delay 60/a
    end
    end" bpm

    Addison Crump

    Posted 2016-01-23T13:59:36.777

    Reputation: 10 763

    4

    AutoIt, 56 bytes

    Func _($0)
    While Sleep(6e4/$0)
    Beep(900,99)
    WEnd
    EndFunc
    

    mınxomaτ

    Posted 2016-01-23T13:59:36.777

    Reputation: 7 398

    4

    Vitsy, 14 bytes

    a6*r/V1m
    <wVO7

    Verbose mode (interpreter coming soon):

    0:                              // a6*r/V1m
    push a; // 10
    push 6;
    multiply top two; // 60
    reverse stack; // bpm on top
    divide top two; // bpm/60
    save/push permanent variable; 
    push 1;
    goto top method; // goes to 1
    1:                              // <wVO7
    go backward; // infinite loop, from the bottom of 1
    wait top seconds;
    save/push permanent variable; // pushes the bpm in terms of seconds of delay
    output top as character;
    push 7;

    Basically, I use the w operator to wait a certain number of seconds as specified by bpm/60, wrapped in an infinite loop. Then, I make noise with the terminal output of ASCII character 7 (BEL).

    Addison Crump

    Posted 2016-01-23T13:59:36.777

    Reputation: 10 763

    Looks nice, but how can I test this? :) – PEAR – 2016-01-23T18:58:12.373

    @PEAR You'll have to download the interpreter (forgot to link it in the title). Save it in a file and run it with java -jar Vitsy.jar <filename>.

    – Addison Crump – 2016-01-23T19:03:11.917

    4

    C#, 118 bytes

    class A{static int Main(string[]a){for(;;System.Threading.Thread.Sleep(60000/int.Parse(a[0])))System.Console.Beep();}}
    

    Basic solution.

    LegionMammal978

    Posted 2016-01-23T13:59:36.777

    Reputation: 15 731

    Why not print ASCII char 7? – Addison Crump – 2016-01-23T19:05:14.613

    @FlagAsSpam It's longer: The system beep uses System.Console.Beep();, and printing the character uses System.Console.Write('<\a character>');. – LegionMammal978 – 2016-01-23T19:09:46.197

    Woah. That's a lot to write a character. – Addison Crump – 2016-01-23T19:10:23.857

    4

    Java, 103 82 bytes

    Thanks to @Justin for shaving off 21 bytes!

    Oh, geez.

    void x(int b)throws Exception{for(;;Thread.sleep(60000/b))System.out.print('\7');}

    Method and golfed version of the sample program.

    Addison Crump

    Posted 2016-01-23T13:59:36.777

    Reputation: 10 763

    Why not System.out.print('\7'); instead of the java.awt.Toolkit.getDefaultToolkit().beep();? – Justin – 2016-01-24T01:52:18.347

    @Justin \ is solely for escaping regex characters. – Addison Crump – 2016-01-24T01:55:15.310

    1no the backslash is an escape sequence. '\7' is the bell character, which makes a sound when it is printed out – Justin – 2016-01-24T01:55:58.573

    @Justin Huh. I've always thrown errors on that (when using double quotes). My mistake. Thanks! :D – Addison Crump – 2016-01-24T11:42:36.183

    3

    GMC-4 Machine Code, 21.5 bytes

    The GMC-4 is a 4-bit computer by a company called Gakken to teach the principles of assembly language in a simplified instruction set and computer. This routine takes input in data memory addresses 0x5D through 0x5F, in big-endian decimal (that is, one digit per nibble).

    The algorithm is basically adding the input to memory and waiting 0.1s, until it's at least 600, and then subtracting 600 and beeping, in an infinite loop. Since the GMC-4 has a bunch of register swap functions but no register copy functions, this is done the hard way.

    In hex (second line is position in memory):

    A24A14A04 80EC AF5A2EF AE5A1EF AD5A0EF 8A6 F2AF09 86ADEEE9F09
    012345678 9ABC DEF0123 4567890 ABCDEF0 123 456789 ABCDEF01234
    

    In assembly:

        tiy 2     ;ld y, 0x2
        AM        ;ld a, [0x50 + y]
        tiy 1
        AM
        tiy 0
        AM
    start:
        tia 0     ;ld a, 0x0
        cal timr  ;pause for (a+1)*0.1 seconds
        tiy F
        MA        ;ld [0x50 + y], a
        tiy 2
        cal DEM+  ;add a to [0x50 + y]; convert to decimal and carry.
        tiy E     ;do the same for the second digit
        MA
        tiy 1
        cal DEM+
        tiy D     ;and the third.
        MA
        tiy 0
        cal DEM+
        tia A
        M+
        jump beep
        jump start
    beep:
        tia 6
        tiy D
        cal DEM-
        cal SHTS  ;'play short sound'
        jump start
    

    Disclaimer:

    I don't actually own a GMC-4. I've meticulously checked this program with documentation from online, but I may have made a mistake. I also don't know the endianness. It looks like the GMC-4 is big-endian, but I'm not sure. If anyone owns a GMC-4 and can verify this/tell me the endianness of the GMC-4, I'd much appreciate it.

    lirtosiast

    Posted 2016-01-23T13:59:36.777

    Reputation: 20 331

    3

    C, 48 bytes

    void f(int b){while(printf(""))Sleep(60000/b);}
                                ^ literal 0x07 here
    

    A Windows-only solution (Sleep() function, to be specific).

    I also (ab)used the fact that printf() returns the number of characters printed to use it as infinite loop condition.

    There IS a character between double-quotes in printf() call, but it is not displayed here for some reason. If in doubt, copy and paste into Sublime Text 2 or Notepad++, the character will be displayed as BEL.

    This started as a C++ solution but it kinda fell into the C-subset of C++ (because, you know, Sleep() is a bit shorter than std::this_thread::sleep_for(std::chrono::milliseconds())) and printf() is shorter than std::cout<<).

    Alexander Revo

    Posted 2016-01-23T13:59:36.777

    Reputation: 270

    3

    AppleScript 94 bytes

    I know I'm pretty late, and this is my first post here, but whatever.

    display dialog""default answer""
    set x to 60000/result's text returned
    repeat
    beep
    delay x
    end

    Ungolfed:

    display dialog "" default answer ""
    set x to 60000 / (result's text returned)
    repeat
        beep
        delay x
    end repeat
    

    You

    Posted 2016-01-23T13:59:36.777

    Reputation: 141

    Hey, new answers :) Unfortunatly I'm unable to try your post unless I have no Mac ;) - but thanks a lot – PEAR – 2016-04-13T16:32:30.220

    @PEAR You're welcome. :) – You – 2016-04-13T16:35:48.757

    Welcome to Programming Puzzles and Code Golf. This is good answer, +1. Please keep answering! – wizzwizz4 – 2016-04-13T17:09:11.080

    2

    VBScript, 113 66 bytes

    a=InputBox("")
    Do
    WScript.Echo(Chr(7))
    WScript.Sleep(60000/a)
    Loop
    

    This program is simple enough; it takes input, echoes the BEL character, and waits. Thanks to Niel for shaving off almost half the program!

    Conor O'Brien

    Posted 2016-01-23T13:59:36.777

    Reputation: 36 228

    What's wrong with WScript.Echo CHR(7)? Also, did you mean 60000? – Neil – 2016-01-23T20:18:25.187

    @Neil Ah, yes. forgot about those.; – Conor O'Brien – 2016-01-23T21:00:33.333

    2

    Ruby, 37 33 bytes

    m=->b{loop{puts"\7"
    sleep 6e1/b}}
    

    Pretty straightforward.

    This is a lambda function. If you wanted 60 bpm, you'd do: m[60].

    Justin

    Posted 2016-01-23T13:59:36.777

    Reputation: 19 757

    Theoretically $><<?\a should also work for the beep. And no need to give a name for your proc (all JavaScript solutions with fat arrow function also leave it unassigned), you can call it anonymously too: ->b{loop{$><<?\a;sleep 6e1/b}}[60]. – manatwork – 2016-01-24T14:32:45.940

    @manatwork I only have Ruby 2.x, so I couldn't test the ?\a; do you have Ruby 1.x? If so, can you test that this works? – Justin – 2016-01-24T17:24:20.443

    Well, I have a Ruby 1.9.3 and the code raises no error with it. But I have another problem with the testing: no beep on my machine. Neither Ruby nor anything else. Set something once, no idea what. – manatwork – 2016-01-24T17:35:09.750

    2

    Japt, 30 bytes

    6e4/U i`?w Au¹o('../1').play()
    

    The ? should be the literal byte 9A. Test it online! (Sorry about the pop-up delaying the first few beats; this will be removed soon.)

    How it works

    6e4/U i"new Audio('../1').play()  // Implicit: U = input bps
    6e4/U                             // Calculate 60000 / U.
          i                           // Set a timed event every that many milliseconds,
           "new Audio('../1').play()  // running this code every time.
                                      // ../1 is the path to the file used in my JS entry.
    

    ETHproductions

    Posted 2016-01-23T13:59:36.777

    Reputation: 47 880

    2

    Perl 5, 36 bytes

    {{$|=print"\a";sleep 60/$_[0];redo}}
    

    A subroutine; use it as

    sub{{$|=print"\a";sleep 60/$_[0];redo}}->(21)
    

    msh210

    Posted 2016-01-23T13:59:36.777

    Reputation: 3 094

    sleep is in seconds, so you can't have more than 60 beeps per minute, not sure if that's a requirement. Also, you can probably keep the same byte count but have a full program by doing something like: $|=<>;{print"\a";sleep 60/$|;redo} (can't test it right now). – ChatterOne – 2016-01-25T20:18:26.640

    @ChatterOne, according to its documentation, you're right about sleep. But it worked for me. – msh210 – 2016-01-28T15:47:00.537

    2

    Mumps, 18 bytes

    R I F  H 60/I W *7
    

    Read the BPM into variable I, then F {with two spaces after} is an infinate loop. Halt for 60 seconds / BPM, then write $CHR(7) {Ascii: BEL} to standard output, giving the audio output required, then restart at the infinite loop.

    zmerch

    Posted 2016-01-23T13:59:36.777

    Reputation: 541

    2

    Java, 321 chars

    Sounds very good. Works only on systems with MIDI support.

    import javax.sound.midi.*;import java.util.*;class A{public static void main(String[] a) throws Exception{int d=new Scanner(System.in).nextInt();Synthesizer b=MidiSystem.getSynthesizer();b.open();MidiChannel c=b.getChannels()[0];c.programChange(116);while(true){c.noteOn(0,100);Thread.sleep((int)(d/.06));c.noteOff(0);}}}
    

    .

    username.ak

    Posted 2016-01-23T13:59:36.777

    Reputation: 411

    Looks nice, but this does not work for me: http://pastebin.com/0CbGYkU0

    – PEAR – 2016-01-30T21:21:43.877

    @PEAR fixed. I forgot a cast. – username.ak – 2016-01-31T12:24:11.020

    @PEAR and an import – username.ak – 2016-01-31T12:33:52.890

    @PEAR, i had swapped some ops because of no sound – username.ak – 2016-01-31T19:02:15.013

    2

    ChucK, 90 bytes

    White noise that is turned on and off every two ticks.

    60./Std.atoi(me.arg(0))*1000=>float s;while(1){Noise b=>dac;s::ms=>now;b=<dac;s::ms=>now;}
    

    Explanation

    60./Std.atoi(me.arg(0)) //Convert the input to an int and divide 60 by it
    *1000                   //Multiply by 1000 (in order to avoid s::second)
    =>float s;              //Store it as a float in variable s
    while(1)                //Forever,
    {Noise b=>dac;          //Connect a noise generator b to the audio output
    s::ms=>now;             //Wait for s milliseconds
    b=<dac;                 //Disconnect b from the audio output
    s::ms=>now;}            //Wait for s milliseconds
    

    This is made to turn on the sound on a beat, then turn it off on the beat after.

    98 93 byte version (fancier)

    White noise played for 10 milliseconds per tick.

    60./Std.atoi(me.arg(0))*1000-9=>float s;while(1){Noise b=>dac;10::ms=>now;b=<dac;s::ms=>now;}
    

    This is made to be a click instead of constant noise being turned on and off.

    The_Basset_Hound

    Posted 2016-01-23T13:59:36.777

    Reputation: 1 566

    1

    Zsh, 32 bytes

    <<<$'\a'
    sleep $[60./$1]
    . $0 $1
    

    Based on the leading bash answer, but sources instead of execs. The TIO link sources $0:a because of how the original file is executed, but it will work without it.

    Try it online!

    GammaFunction

    Posted 2016-01-23T13:59:36.777

    Reputation: 2 838

    You're late to the party but this looks like really fine solution! – PEAR – 2019-08-18T09:28:36.713

    I know I'm late, but I just felt like golfing today. Decided to check on the music tag for fun, and found this challenge. Good one, btw! – GammaFunction – 2019-08-18T09:45:39.380

    1

    Jolf, 7 bytes, noncompeting

    I added sounds after this very fine challenge was made.

    TΑa/Αaj
    T       set an interval
     Αa      that plays a short beep (Α is Alpha)
       /Αaj  every 60000 / j (the input) seconds. (Αa returns 60000)
    

    If you so desire to clear this sound, take note of the output. Say that number is x. Execute another Jolf command ~CP"x", and the interval will be cleared.

    Conor O'Brien

    Posted 2016-01-23T13:59:36.777

    Reputation: 36 228

    0

    SmileBASIC, 26 bytes

    INPUT B$BGMPLAY@8T+B$+"[C]
    

    It can play any general midi instrument, though anything above 9 will use more bytes.

    12Me21

    Posted 2016-01-23T13:59:36.777

    Reputation: 6 110

    0

    Stax, 17 bytes

    ü7»♥O╚⌂╥☻≈OyM╜Δ∩`
    

    or, unpacked:

    4|A48*x/W2|A]pc{| }*
    

    The program outputs bytes that, when fed through the command line tool aplay with default setting, produce a metronome noise. The input is used as bpm

    example:

    example-stax-interpreter metronome.stax -i "60" | aplay
    

    You should hear a horrible beeping noise at the desired bpm

    user89655

    Posted 2016-01-23T13:59:36.777

    Reputation: 31

    0

    Bash + bc + ><>, 44 bytes

    Playing on the fact that the ><> interpreter lets you define a tick time :

    python fish.py -t $(bc -l<<<"2/$1/60") -c 7o
    

    The ><> code is 7o and should output the BEL character, producing a system beep. It will loop until interrupted.
    The -t value is set to (2 / RPM ) / 60 so that the whole code is played RPM * 60 times per second.

    Aaron

    Posted 2016-01-23T13:59:36.777

    Reputation: 3 689

    Thanks a lot for a new answer after some amount of time after publishing. Doesn't work for me :( Not sure if a problem of my system or something else. I downloaded the fish.py from GitHub and executed your commad (openSUSE). Got this error:

    (standard_in) 1: syntax error usage: fish.py [-h] (<script file> | -c <code>) [<options>] fish.py: error: argument -t/--tick: expected one argument – PEAR – 2016-04-13T16:40:33.947

    Have you got bc installed? It looks like the $(bc -l<<<"2/$1/60") did not produce any output. I'll add it to the list of languages of the answer. I haven't been able to fully test my answer yet, so there might be some kind of error too. – Aaron – 2016-04-13T16:53:56.623