【Delphi】模拟心电图声音,存粹好玩,记录下来

  在做程序开发的时候,需要一种连续的蜂鸣声音提示,比如心电图声音,输血时蜂鸣器声音,为了方便,专门做了一个蜂鸣器函数,相信有人需要。

{ sensor 2025-09-08
  蜂鸣器线程函数,可以实现心电图蜂鸣声

  原则:蜂鸣器同时只能有一个在响,二次打开蜂鸣器不会产生两个

  1. 启动蜂鸣器
     Start_Alarm_Once(Hz : Word; Beep_duration : Word; Interval_duration : Word; Continuous_duration : Word);

     Hz : 蜂鸣器的频率, 一般: 800-2000
     Beep_duration : 蜂鸣时长,单位:毫秒 默认:300
     Interval_duration : 间隔时长,单位:毫秒 默认:400
     Continuous_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;

interface

uses
  Winapi.Windows,
  System.SysUtils,
  System.Threading,
  System.Generics.Collections,
  System.DateUtils,
  System.Classes,
  Forms,
  IdThreadComponent;

type
  TAlarm_Thread = class
    private
      //停止 蜂鸣器线程 运行标志,默认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);

    public

      constructor 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;

var
  myAlarm_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;
begin
  Result := 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);
begin
  if 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);
begin
  if Assigned(myAlarm_Thread) then
    begin
      myAlarm_Thread.Stop_Alarm_Thread;
      while not myAlarm_Thread.Stoped do
        begin
          Application.ProcessMessages;
          sleep(10);
        end;
      myAlarm_Thread.Free;
    end;
end;

procedure Stop_Alarm_Once;
begin
  if Assigned(myAlarm_Thread) then
    begin
      myAlarm_Thread.Stop_Alarm_Thread;
      while not myAlarm_Thread.Stoped do
        begin
          Application.ProcessMessages;
          sleep(10);
        end;
      myAlarm_Thread.Free;
      myAlarm_Thread := nil;
    end;
end;


{ TAlarm_Thread }

procedure TAlarm_Thread.Alarm_ThreadOnException(Sender: TIdThreadComponent;   AException: Exception);
begin

end;

procedure TAlarm_Thread.Alarm_ThreadOnRun(Sender: TIdThreadComponent);
begin
  Winapi.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);
begin
  F_Stoped := True;
end;

constructor TAlarm_Thread.Create(Hz,Beep_duration,Interval_duration,Continuous_duration : Word);
begin
  F_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 then
     F_StopTime := 0
  else
     F_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;
begin
  Alarm_Thread.Free;
end;

procedure TAlarm_Thread.Start_Alarm_Thread;
begin
  Alarm_Thread.start;
  F_Stoped := False;
end;

procedure TAlarm_Thread.Stop_Alarm_Thread;
begin
  Alarm_Thread.stop;
end;

end.

谁用谁知道!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海纳老吴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值