Is there a default interval between each ASCII Bell chime?

104 Views Asked by At

Here is a simple metronome script in Bash using ASCII bell.

#!/bin/bash

read -s tempo
interval=$(bc -l <<< "scale=4; 60/$tempo")

echo -n "tempo is $tempo  interval is $interval seconds"

while true
do
        echo -en "\a"
        sleep $interval
done

The bug I'm encountering:

When tempo is set at more than 60, metronome script will start always at 120BPM initially, and only update to the correct tempo fast or slower after I interrupt it by hitting a random key.

I also tried it in Java and encountered the same issue.

import java.util.Scanner;
import java.lang.Runtime;
import java.io.*;

public class Metronome {

  public static void main(String[] args) throws InterruptedException { 

    Scanner in = new Scanner(System.in);

    double tempo = in.nextDouble();
    double interval = (60 / tempo) *1000;


    while (true) {
        System.out.println("\u0007");
        Thread.sleep((long) interval);
    }

  }

}

Is there a system default interval time that is set between bell chimes? How come it changed speed after I interrupt the thread/job?

1

There are 1 best solutions below

2
On

I don't think there is an enforced minimal interval between two beeps, but each beep itself has a certain duration, which is smaller than a second, but at least long enough that you hear the beep as a beep and not as a click. If you send two beeps in sequence, and the sending interval is shorter than the beep duration, the \0007 characters are simply queued up in a buffer and processed in sequence. The effect is as if you would send text to the terminal faster than it can be displayed.