Skip to content

Instantly share code, notes, and snippets.

@icanwalkonwater
Last active November 19, 2018 08:42
Show Gist options
  • Save icanwalkonwater/706aa3aa12e4b0107266ced1e05d7887 to your computer and use it in GitHub Desktop.
Save icanwalkonwater/706aa3aa12e4b0107266ced1e05d7887 to your computer and use it in GitHub Desktop.
Play ~aproximatively~ the melody when you open a chest in zelda games with the beeper of your pc. (you need to run it as root).
#!/bin/sh
gcc zelda_beep.c -o zelda_beep
# set the setuid bit to allow anyone to run this as root
sudo chown root:root zelda_beep
sudo chmod 4755 zelda_beep
./zelda_beep
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/kd.h>
#include <zconf.h>
#include <stdio.h>
#ifndef CLOCK_TICK_RATE
#define CLOCK_TICK_RATE 1193180.0
#endif
#define N_HIGH_G 1567.98
#define N_HIGH_FS 1479.98
#define N_HIGH_DS 1244.51
#define N_MIDDLE_A 440.0
#define N_MIDDLE_GS 415.30
#define N_HIGH_E 1318.51
#define N_HIGH_GS 1661.22
#define N_HIGH_C 1046.50
int play_beep(int fd, int millis, float freq) {
if (ioctl(fd, KIOCSOUND, (int) (CLOCK_TICK_RATE / freq)) < 0) {
perror("Failed to beep");
return 1;
}
printf("Playing freq %f\n", freq);
usleep((__useconds_t) (1000 * millis));
ioctl(fd, KIOCSOUND, 0);
return 0;
}
int main() {
int console_fd = open("/dev/console", O_WRONLY);
if (console_fd == -1) {
perror("Failed to open /dev/console");
return 1;
}
play_beep(console_fd, 130, N_HIGH_G);
play_beep(console_fd, 130, N_HIGH_FS);
play_beep(console_fd, 130, N_HIGH_DS);
play_beep(console_fd, 130, N_MIDDLE_A);
play_beep(console_fd, 130, N_MIDDLE_GS);
play_beep(console_fd, 130, N_HIGH_E);
play_beep(console_fd, 130, N_HIGH_GS);
play_beep(console_fd, 800, N_HIGH_C);
close(console_fd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment