使用 Perl 和 FluidSynth 创建系统状态的实时音乐组合。了解如何将各种系统监视数据集成为一种生成和声、MIDI 控制的音频合成。探究可帮助您监控和管理计算环境的音频信息方法和配置。 有很多种可视化信息监控程序可用于评估计算机环境的运行状况。可以使用从简单文本显示,到实时图表和 3D 彩色图表的一切手段来协助您诊断个人计算设备、服务器计算设备或网络计算设备的问题。chordStats 将新的接口通道添加到系统监视设置中 —— 通过音调、音色以及和声传递信息。 大约在 1998 年,这种功能破土而出!Network Auralizer 被开发用于提供一种 “环境式” 音效环境,以辅助管理员来判断网络的基准和紊乱状态。使用根据特定事件回放的声音效果,管理员可以专注于网络的正常音效(例如,水浪声表示一般负载,鸟叫声表示网络事件),并在出问题时通过环境音效的变化立即得知情况。 chordStats 用添加音调的方法生成类似的声音环境,配备声音特征,并且根据系统负载创建和声。在本文中,您将创建一个简单的 Perl 脚本将通知事件发送给 FluidSynth,强制将各种系统事件解释为悦耳的声音,文中还探讨了增强音乐监控环境的未来选择。 要求 硬件 本文是部分基于 Intel® Pentium® 4 及 256 MB RAM 开发的。由于 FluidSynth MIDI 软件合成非常耗费资源,因此请不要尝试在低于 Pentium III 及 256 MB RAM 配置的机器上执行。还需要使用声卡。为实现本文的目的,我们假定用户的声卡中没有硬件 MIDI 波表合成器,并因此而使用了软件合成器。如果声卡中硬件合成器或需要将 chordStats 与外部硬件结合使用,请参阅 参考资料 获得帮助入门的链接。 软件 建议使用最新版本(2.4 或更高版本)的 Linux®,以及 Perl 和 FluidSynth。有关 FluidSynth 应用程序,请参阅 参考资料。另外建议您选用 SoundFonts。下载 部分附带了专用于打击乐器(管钟、钢琴、木琴)的内置 SoundFont。Internet 上有很多免费的 SoundFonts 资源(请参阅 参考资料)。 我们使用 vmstat 程序进行简单的系统状态监视。vmstat 是很多 Linux 发行版的标准配置,它提供了三种形式的 CPU 负载,一般磁盘块出入以及监视很多其他系统功能的能力。 设置和配置示例 FluidSynth 设置和检验 安装 FluidSynth 并下载示例 SoundFont 后,用命令 fluidsynth Hammered_Instruments.sf2 启动 FluidSynth 程序。您将看到类似以下内容的输出: 清单 1. FluidSynth 输出
lash_open_socket: could not look up host 'localhost': /Servname not supported for ai_socktypelash_open_socket: could not connect to host 'localhost', service '14541'lash_comm_connect_to_server: could not create server connectionlash_init: could not connect to server 'localhost' - disabling LASHlash_init: LASH_START_SERVER unset, not attempting to start server automaticallyfluidsynth: warning: Failed to pin the sample data to RAM; swapping is possible.ALSA lib timer_hw.c:269:(snd_timer_hw_open) /extended read is not supported (SNDRV_TIMER_IOCTL_TREAD)fluidsynth: warning: Requested a period size of 64, got 940 insteadfluidsynth: ALSA driver: Using format s16, rw, interleavedFluidSynth version 1.0.7Copyright (C) 2000-2006 Peter Hanappe and others.Distributed under the LGPL license.SoundFont(R) is a registered trademark of E-mu Systems, Inc.Type 'help' for information on commands and 'help help' for help topics.> |
不必担心这些警告消息。如果看到 > 符号,则 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 statususe strict; my $vmStatCmd = "vmstat 1"; # run vmstat every secondmy $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" ){ $fields{$_} = $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 = <INPIPE> ){ # 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 "reset/n" } $totalTime ++; my $note = ""; my @currLine = split " ", $statLine; # user cpu usage $note = $currLine[ $fields{us} ]; sendNote( $note, 14, 12, 96 ); # conglomerate disk i/o fields to one stat $note = $currLine[ $fields{bi} ] + $currLine[ $fields{bo} ]; 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 pipeclose(INPIPE); |
如果经过了 10 秒钟时间,请将一个重置事件发送给 FluidSynth。这将清除仍在处理的所有剩余通知,即使它们已经减弱为听不见的音量级别。变量初始化后,与 us(用户 CPU 使用量)对应的通知将被 sendNote 命令激活。由于 us 字段的值总是在 0 到 100 之间,因此无需进行其他处理。只要使用 sendNote 子例程在通道 14 中发送通知速率,其中最低速率为 12,最高速率为 96。 Linux联盟收集整理 |