用机器生成的音乐来监控Linux计算机

ZDNet软件频道 时间:2008-06-30 作者:Nathan Harrington | 赛迪网 我要评论()
本文关键词:命令行 Linux 技巧
使用 Perl 和 FluidSynth 创建系统状态的实时音乐组合。了解如何将各种系统监视数据集成为一种生成和声、MIDI 控制的音频合成。探究可帮助您监控和管理计算环境的音频信息方法和配置。

不必担心这些警告消息。如果看到 > 符号,则 FluidSynth 已经准备好开始生成声音。尝试使用 noteon 5 77 100 在通道 5 中创建一个速率为 77、音量级别为 100 的通知。如果可以听到电子钢琴声,那么您就已经准备好继续执行下一步。键入 quit 或使用 Ctrl+C 组合键退出 FluidSynth。如果声音不可用或者收到错误消息,则请确保声音服务器已经启动并且混音器的设置都正确。

向 FluidSynth 发送多个通知事件时,您可能会注意到一些声音波动 —— 就像音乐 CD 跳帧一样。采用命令 fluidsynth Hammered_Instruments.sf2 -c10000 -z10000 增加音频缓冲区的数目和每个缓冲区的大小,以便缓解此问题。这将启动有 10,000 个音频缓冲区且每个缓冲区大小为 10,000 的 FluidSynth —— 这个空间很大,足以减少波动。

chordStats.pl 程序

一般策略

节拍、音色、和音以及通知速率的选择综合了艺术与科学两个方面,而这种综合的方式已经远远超出本文讨论范围。为了简化开发及信息分发,此程序的主要特性包括 1-Hz 刷新率和基于八度音阶的通知标准。vmstat 程序为基本系统数据提供了一个简单接口,而且创建了一个 1-Hz “心跳” 并以它为节拍。

清单 2. 主程序参数

#!/usr/bin/perl -w
# chordStats.pl - create music based on system status
use strict;
my $vmStatCmd = "vmstat 1"; # run vmstat every second
my $totalPackets = 0; # total of packets received and transmitted
my $lineCount = 0; # count number of vmstat output lines
my %fields = ();
my $count = 0;
# the field headers in the vmstat output, useful for referring to them by name
for( split " ", "r b swpd free buff cache si so bi bo in cs us sy id wa" ) = $count;
$count++;
}
# buffering output must be turned off because fluidsynth does not appear to
# accept buffered input from stdin
$|=1;

在脚本的开头,我们选择 vmstat 1 作为要被执行的命令并且每秒读取一次。为各次读取之间记录的信息包总数设置一个变量并记录从 vmstat 程序中读取到的行数,接下来定义标题。每秒都会从 vmstat 程序中读取字段标题 bi(磁盘块入)、bo(磁盘块出)和 us(用户 CPU 使用量)。字段散列允许稍后在程序中按名称引用这些数据字段。请注意 $|=1 行。如果删除此行,您将会遇到一些难以诊断的行为;还需要为缓冲而头疼!

清单 3. 主程序开始

# open the vmstat program to read from
open( INPIPE, "$vmStatCmd |" ) || die "can't read from vmstat";
# wait while the fluidsynth program opens
sleep(3);
while(my $statLine = ){
# ignore the header display, and the fieldname display lines
if( $statLine !~ /----/ && $statLine !~ /swpd/ ){
# the first line of vmstat data is a recent average, ignore
if( $lineCount > 2 ){

代码的下一部分将通过 vmstat 命令创建一个管道,程序将在其中每秒读取一次数据。等待几秒待 FluidSynth 程序激活后,即可开始处理 vmstat 输出。输出的前三行将被忽略,因为它们分别包含分隔符、标题信息和最新的平均值。

清单 4. 主程序通知处理

# reset wavetable synthesis
if( $totalTime % 10 == 0 ){ print "resetn" }
$totalTime ++;
my $note = "";
my @currLine = split " ", $statLine;
# user cpu usage
$note = $currLine[ $fields ];
sendNote( $note, 14, 12, 96 );
# conglomerate disk i/o fields to one stat
$note = $currLine[ $fields ] + $currLine[ $fields ];
if( $note > 1000 ){ $note = 1000; }
$note = $note/10;
sendNote( $note, 8, 12, 96 );
# network throughput on eth0
$note = getNetworkStats();
sendNote( $note, 5, 12, 84 );
}#if not first 3 lines to ignore
}#if not a header line
$lineCount++;
}#while reading the pipe
close(INPIPE);

如果经过了 10 秒钟时间,请将一个重置事件发送给 FluidSynth。这将清除仍在处理的所有剩余通知,即使它们已经减弱为听不见的音量级别。变量初始化后,与 us(用户 CPU 使用量)对应的通知将被 sendNote 命令激活。由于 us 字段的值总是在 0 到 100 之间,因此无需进行其他处理。只要使用 sendNote 子例程在通道 14 中发送通知速率,其中最低速率为 12,最高速率为 96。


百度大联盟认证黄金会员Copyright© 1997- CNET Networks 版权所有。 ZDNet 是CNET Networks公司注册服务商标。
中华人民共和国电信与信息服务业务经营许可证编号:京ICP证010391号 京ICP备09041801号-159
京公网安备:1101082134