穷人的特雷门琴

作者:Don Marti

如果您有兴趣体验演奏特雷门琴,但又不想花很多钱或自己构建套件,可以试试由 Seth David Schoen 编写的 “穷人的特雷门琴” (PMT)。

PMT 将配备 802.11b 网卡的笔记本电脑变成类似特雷门琴的乐器,它使用网卡报告的信号强度来控制音高。 要尝试它,首先使用命令 cc -o ttone -lm ttone.c 编译这个名为 ttone 的 C 程序。

列表 1: ttone.c
#include <math.h>
#include <linux/kd.h>
const int A = 440;
const float r = 1.05946;
int pitch(int base, int observed){
    return (int) A * pow(r, (observed-base));
    }
int main(int argc, char *argv[]){
    int base, observed;
    if (strcmp("off", argv[1])){
        base = atoi(argv[1]);
        observed = atoi(argv[2]);
        ioctl(0, KIOCSOUND, 1190000 / pitch(base, observed) );
    } else {
        ioctl(0, KIOCSOUND, 0);
    };
}

将 ttone 可执行文件放在 PATH 环境变量包含的目录中。

现在运行以下名为 pmt.sh 的 shell 脚本,并移动到 802.11b 接入点的范围内。 您可以通过靠近或远离接入点,或者像特雷门琴演奏者那样将手移到 802.11b 天线上来改变音高。

列表 2: pmt.sh
#!/bin/sh
# Poor Man's Theremin
m=100
oldQ=foo
[ $1 ] && m=$1
while :
do
Q=$(iwconfig 2</dev/null | grep Link.Quality | cut -d: -f2 | cut -d/ -f1)
if [ $oldQ != $Q ]
then
    ./ttone $m $Q
    fi
    oldQ=$Q
    done

穷人的特雷门琴没有真正特雷门琴的音量控制功能,并且音高的变化是离散的,而不是连续的。 实现这些功能留给读者作为练习。

Don MartiLinux Journal 的主编。

电子邮件: dmarti@ssc.com

加载 Disqus 评论