Skip to content

Instantly share code, notes, and snippets.

@przemoc

przemoc/siemens.gawk

Last active Feb 6, 2018
Embed
What would you like to do?
Siemens ringtone converter v. 0.3 // taken from pcspk 0.0.6: http://wiki.przemoc.net/projects/pcspk
#!/usr/bin/gawk -f
# SPDX-License-Identifier: GPL-2.0-only
## (C) Copyright 2007-2008 Przemyslaw Pawelczyk <przemoc@gmail.com>
##
## This script is licensed under the terms of the GNU GPL v2 only license.
## https://www.gnu.org/licenses/gpl-2.0.html
#
# Siemens ringtone converter v. 0.3
#
# Usage:
# siemens.gawk [-v bpm=XXX] [-v oct=Y] siemens_ringtone_file | pcspk -n
BEGIN {
IGNORECASE = 1
BPM = 100; # default BPM
OCT = 2; # default octave shift
_ord_init();
is = "CDFGA";
es = "degaB";
ORS = " ";
}
/^#.*bpm=([0-9]+)/ {
match($0, /bpm=([0-9]+)/, b);
if ((b[1] > 0) && !(bpm > 0))
bpm = b[1];
}
/^#.*oct=([0-9]+)/ {
match($0, /oct=([0-9]+)/, b);
if ((b[1] > 0) && !(oct > 0))
oct = b[1];
}
# _ord_init is taken from
# http://www.gnu.org/software/gawk/manual/gawk.html#Ordinal-Functions
function _ord_init( low, high, i, t) {
low = sprintf("%c", 7); # BEL is ascii 7
if (low == "\a") { # regular ascii
low = 0;
high = 127;
} else if (sprintf("%c", 128 + 7) == "\a") { # ascii, mark parity
low = 128;
high = 255;
} else { # ebcdic(!)
low = 0;
high = 255;
}
for (i = low; i <= high; i++) {
t = sprintf("%c", i);
_ord_[t] = i;
}
}
function parse(input) {
split(input, array, "[ \t]+");
for (i = 1; i in array; i++) {
if (array[i] !~ /^([CDEFGAHBP](is)?)([0-9])?\(1\/([0-9]+)\)/)
continue;
match(array[i], /^([CDEFGAHBP])(is)?([0-9])?\(1\/([0-9]+)\)/, s);
if (s[2] != "")
s[1] = substr(es, index(is, s[1]), 1);
else
s[1] = toupper(s[1]);
s[3] += oct;
if (s[3] < 0)
s[3] = 0;
s[4] = s[4] - 1;
if(s[4] >= 10)
s[4] = sprintf("%c", _ord_["A"] + s[4] - 10);
print s[1] s[3] s[4]
}
}
/^[^#]/ {
if (!(notfirst)) {
if (!(bpm > 0))
bpm = BPM;
if (!(oct > 0))
oct = OCT;
printf "%03d%s", bpm, ORS;
notfirst = 1;
}
parse($0);
}
@przemoc

This comment has been minimized.

Copy link
Owner Author

@przemoc przemoc commented Feb 6, 2018

commit eb26af0170384fe6a483a9186ada21da50273533
Author:     Przemyslaw Pawelczyk <przemoc@gmail.com>
AuthorDate: 2018-02-06 14:06:25 +0100

    siemens.gawk: Simplify copyright+license notices.

commit 8e37182a7fa0d0681bb513be820112ce83a883ed
Author:     Przemyslaw Pawelczyk <przemoc@gmail.com>
AuthorDate: 2018-02-06 14:08:52 +0100

    Add SPDX License Identifier.

    The Software Package Data Exchange (SPDX) is a good initiative, it has
    matured over time and deserves accelerated adoption in open-source.

    https://spdx.org/learn
    https://spdx.org/using-spdx
    https://spdx.org/license-list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment