【Delphi】模拟心电图声音,存粹好玩,记录下来
在做程序开发的时候,需要一种连续的蜂鸣声音提示,比如心电图声音,输血时蜂鸣器声音,为了方便,专门做了一个蜂鸣器函数,相信有人需要。
{ sensor 2025-09-08蜂鸣器线程函数,可以实现心电图蜂鸣声原则:蜂鸣器同时只能有一个在响,二次打开蜂鸣器不会产生两个1. 启动蜂鸣器Start_Alarm_Once(Hz : Word; Beep_duration : Word; Interval_duration : Word; Continuous_duration : Word);Hz : 蜂鸣器的频率, 一般: 800-2000Beep_duration : 蜂鸣时长,单位:毫秒 默认:300Interval_duration : 间隔时长,单位:毫秒 默认:400Continuous_duration : 持续时长,单位:秒 默认:120 (2分钟) 0 表示一直蜂鸣2. 停止蜂鸣器Stop_Alarm_once;//如果期望多个蜂鸣器同时蜂鸣,则需要定义多个蜂鸣器对象变量3. 启动蜂鸣器Alarm_Thread1 : TAlarm_Thread;Alarm_Thread1 := Start_Alarm(Hz : Word; Beep_duration : Word; Interval_duration : Word; Continuous_duration : Word);Alarm_Thread2 : TAlarm_Thread;Alarm_Thread2 := Start_Alarm(Hz : Word; Beep_duration : Word; Interval_duration : Word; Continuous_duration : Word);...4. 停止蜂鸣器Stop_Alarm(Alarm_Thread1);Stop_Alarm(Alarm_Thread2);...5. 心电图蜂鸣器模拟声Start_Alarm_Once(1600,300,600);5.1 输血模拟声音Start_Alarm_Once(500,300,600);6. 蜂鸣3分钟后自动停止(不建议i这样使用,会造成内存泄露),建议明确使用Stop_Alarm函数,配对使用Start_Alarm_Once(1600,300,600,300);7. 有提篮的声音Start_Alarm_Once(1600,1000,800);}
unit uAlarm_Thread;interfaceusesWinapi.Windows,System.SysUtils,System.Threading,System.Generics.Collections,System.DateUtils,System.Classes,Forms,IdThreadComponent;typeTAlarm_Thread = classprivate//停止 蜂鸣器线程 运行标志,默认True,False表示已经停止F_Stoped : Boolean;F_StopTime : TDateTime; //线程停止的时间,如果是0,表示永不停止//1 读卡器线程F_Hz : Word; //蜂鸣器频率F_Beep_duration : Word; //蜂鸣时长F_Interval_duration : Word; //蜂鸣间隔时长F_Continuous_duration : Word; //蜂鸣持续时长Alarm_Thread : TIdThreadComponent;//1.1 读卡器线程 需要绑定的过程procedure Alarm_ThreadOnRun(Sender: TIdThreadComponent);procedure Alarm_ThreadOnException(Sender: TIdThreadComponent; AException: Exception);procedure Alarm_ThreadOnTerminate(Sender: TIdThreadComponent);publicconstructor Create(Hz,Beep_duration,Interval_duration,Continuous_duration : Word);destructor Destroy;// 启动读卡器线程procedure Start_Alarm_Thread;// 停止读卡器线程procedure Stop_Alarm_Thread;property Stoped : Boolean read F_Stoped; //只读属性end;varmyAlarm_Thread : TAlarm_Thread;//告警ErrAlarm_Thread : TAlarm_Thread;//打开蜂鸣器
function Start_Alarm(Hz : Word; Beep_duration : Word; Interval_duration : Word; Continuous_duration : Word = 0) : TAlarm_Thread;procedure Start_Alarm_Once(Hz : Word; Beep_duration : Word; Interval_duration : Word; Continuous_duration : Word = 0);//关闭蜂鸣器
procedure Stop_Alarm(myAlarm_Thread : TAlarm_Thread);procedure Stop_Alarm_Once;implementation//打开蜂鸣器
function Start_Alarm(Hz : Word; Beep_duration : Word; Interval_duration : Word; Continuous_duration : Word) : TAlarm_Thread;
beginResult := TAlarm_Thread.Create(Hz,Beep_duration,Interval_duration,Continuous_duration);Result.Start_Alarm_Thread;
end;procedure Start_Alarm_Once(Hz : Word; Beep_duration : Word; Interval_duration : Word; Continuous_duration : Word);
beginif Assigned(myAlarm_Thread) then Exit;myAlarm_Thread := TAlarm_Thread.Create(Hz,Beep_duration,Interval_duration,Continuous_duration);myAlarm_Thread.Start_Alarm_Thread;
end;//关闭蜂鸣器
procedure Stop_Alarm(myAlarm_Thread : TAlarm_Thread);
beginif Assigned(myAlarm_Thread) thenbeginmyAlarm_Thread.Stop_Alarm_Thread;while not myAlarm_Thread.Stoped dobeginApplication.ProcessMessages;sleep(10);end;myAlarm_Thread.Free;end;
end;procedure Stop_Alarm_Once;
beginif Assigned(myAlarm_Thread) thenbeginmyAlarm_Thread.Stop_Alarm_Thread;while not myAlarm_Thread.Stoped dobeginApplication.ProcessMessages;sleep(10);end;myAlarm_Thread.Free;myAlarm_Thread := nil;end;
end;{ TAlarm_Thread }procedure TAlarm_Thread.Alarm_ThreadOnException(Sender: TIdThreadComponent; AException: Exception);
beginend;procedure TAlarm_Thread.Alarm_ThreadOnRun(Sender: TIdThreadComponent);
beginWinapi.windows.Beep(F_Hz,F_Beep_duration);Sleep(F_Interval_duration);if (F_StopTime <> 0) and (Now > F_StopTime) then(Sender as TIdThreadComponent).stop;
end;procedure TAlarm_Thread.Alarm_ThreadOnTerminate(Sender: TIdThreadComponent);
beginF_Stoped := True;
end;constructor TAlarm_Thread.Create(Hz,Beep_duration,Interval_duration,Continuous_duration : Word);
beginF_Stoped := True;//1 创建 读卡器线程 对象F_Hz := Hz;F_Beep_duration := Beep_duration;F_Interval_duration := Interval_duration;F_Continuous_duration := Continuous_duration;if F_Continuous_duration = 0 thenF_StopTime := 0elseF_StopTime := IncSecond(Now,F_Continuous_duration); //需要停止的时间Alarm_Thread := TIdThreadComponent.Create(nil);Alarm_Thread.Loop := True;Alarm_Thread.OnRun := Alarm_ThreadOnRun;Alarm_Thread.OnException := Alarm_ThreadOnException;Alarm_Thread.OnTerminate := Alarm_ThreadOnTerminate;
end;destructor TAlarm_Thread.Destroy;
beginAlarm_Thread.Free;
end;procedure TAlarm_Thread.Start_Alarm_Thread;
beginAlarm_Thread.start;F_Stoped := False;
end;procedure TAlarm_Thread.Stop_Alarm_Thread;
beginAlarm_Thread.stop;
end;end.
谁用谁知道!