How to play sounds on the PC Speaker ?

CK CK 3776 
Created at
Updated at  
523 0 0

Here's an example of how to play simple sounds on the PC speaker

 

procedure SetPort(address, Value: Word) ;
var
  bValue: Byte;
begin
  bValue := trunc(Value and 255) ;
  asm
    mov dx, address
    mov al, bValue
    out dx, al
  end;
end;

function GetPort(address: Word): Word;
var
  bValue: Byte;
begin
  asm
    mov dx, address
    in al, dx
    mov bValue, al
  end;
  GetPort := bValue;
end;

procedure Sound(aFreq, aDelay: Integer) ;

  procedure DoSound(Freq: Word) ;
  var
    B: Byte;
  begin
    if Freq > 18 then
    begin
      Freq := Word(1193181 div Longint(Freq)) ;
      B := Byte(GetPort($61)) ;

      if (B and 3) = 0 then
      begin
        SetPort($61, Word(B or 3)) ;
        SetPort($43, $B6) ;
      end;

      SetPort($42, Freq) ;
      SetPort($42, Freq shr 8) ;
    end;
  end;

  procedure Delay(MSecs: Integer) ;
  var
    FirstTickCount: LongInt;
  begin
    FirstTickCount := GetTickCount;
    repeat
      Sleep(1) ;
      //or use Application.ProcessMessages instead of Sleep
    until ((GetTickCount - FirstTickCount) >= Longint(MSecs)) ;
  end;

begin
  if Win32Platform = VER_PLATFORM_WIN32_NT then
  begin
    Windows.Beep(aFreq, aDelay) ;
  end
  else
  begin
    DoSound(aFreq) ;
    Delay(aDelay) ;
  end;
end;

procedure NoSound;
var
  Value: Word;
begin
  if not (Win32Platform = VER_PLATFORM_WIN32_NT) then
  begin
    Value := GetPort($61) and $FC;
    SetPort($61, Value) ;
  end;
end;

{
// Example:

procedure TForm1.Button1Click(Sender: TObject) ;
begin
  Sound(500, 1000) ;
  Sound(700, 1000) ;
  Sound(900, 1000) ;
  NoSound;
end;
}
Tags Hide from Taskbar SetPort Sound Sound Play by PC Speaker VER_PLATFORM_WIN32_NT chkdsk Facebook X
Comments 0
Similar posts
  1. How to install an INF file using Delphi ?
    460
  2. How to determine the output of a console application ?
    449
  3. Get text from the control at pos x,y on screen
    464
  4. Get dimensions of usable space on monitor (work area)
    486
  5. Get system info (free memory, etc)
    402
  6. How to Get Filenames from Clipboard ?
    1,500
  7. How to Get Windows OS Version ?
    424
  8. Get Windows Temp directory
    652
  9. Disable ALT+TAB, CTRL+ESC, CTRL+ALT+DEL
    1,819
  10. Display Standard Windows Properties dialog
    1,125
  11. Disable Mouse and Keyboard from Delphi Code
    787
  12. Detecting Drive Types
    466
  13. Detecting and preventing Windows shut down
    432
  14. Copying Group of Files using Delphi with Standard Animation Dialog (SHFileOperation)
    411
  15. Controling sound volume from code
    417
  16. How to Close Another Application by Windows Caption
    384
  17. How to Change the Windows Start button bitmap
    421
  18. How to capture the output from a DOS (command/console) Window
    960
  19. How to Activate/Deactivate the Screen Saver
    855
  20. How to remove your application from TaskBar
    655
  21. How to track a user's idle time
    587
  22. How to know whether the font is TrueTypeFont or not
    373
  23. List Devices (LPT, COM ports, ...)
    477
  24. How to restrict mouse movement
    405
  25. How to send char as message to another application
    536
  26. How to save text from Clipboard to a file
    564
  27. How to show window contents while dragging
    449
  28. How to use TTF Font Without Installing
    1,112
  29. Window flashing
    378
  1. Delphi for .Net Code Folding keyboard shortcuts
    523
  2. How to encrypt a string
    461
  3. Creating thumbnail images
    859
  4. Technology sharing related with software development. Specially content will be focused to programming language, and embedded systems.
    846
  5. How to speed up access to the RichTextBox Lines property
    825
  6. Window flashing
    378
  7. How to use TTF Font Without Installing
    1,112
  8. How to show window contents while dragging
    449
  9. How to save text from Clipboard to a file
    564
  10. How to send char as message to another application
    536
  11. How to restrict mouse movement
    405
  12. List Devices (LPT, COM ports, ...)
    477
  13. How to know whether the font is TrueTypeFont or not
    373
  14. How to track a user's idle time
    587
  15. How to remove your application from TaskBar
    655
  16. How to install an INF file using Delphi ?
    460
  17. How to determine the output of a console application ?
    449
  18. Get text from the control at pos x,y on screen
    464
  19. Get dimensions of usable space on monitor (work area)
    486
  20. Get system info (free memory, etc)
    402
  21. How to Get Filenames from Clipboard ?
    1,500
  22. How to Get Windows OS Version ?
    424
  23. Get Windows Temp directory
    652
  24. Disable ALT+TAB, CTRL+ESC, CTRL+ALT+DEL
    1,819
  25. Display Standard Windows Properties dialog
    1,125
  26. Disable Mouse and Keyboard from Delphi Code
    787
  27. Detecting Drive Types
    466
  28. Detecting and preventing Windows shut down
    432
  29. Copying Group of Files using Delphi with Standard Animation Dialog (SHFileOperation)
    411
  30. Controling sound volume from code
    417
Recently updated
  1. How do I create animated background for Google Meet?
    19,005
  2. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    192
  3. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    75
  4. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    151
  5. Array of arguments passed to script in PHP
    83
  6. Invalid Auction Storms in Ad Tech
    91
  7. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    194
  8. The Two Clocks of Ad Server Architecture
    61
  9. Frequency Cap Architecture Overview
    505
  10. Collecting Proof-of-Play from 100,000 Screens
    139
  11. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    276
  12. What is RedisVL?
    102
  13. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    218
  14. TPU & GPU Cluster Management Tools
    229
  15. The Concept of Scaffolding in AI Agents
    217
  16. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    568
  17. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    246
  18. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    321
  19. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    196
  20. Machine Learning: Training vs. Ranking
    200
  21. The Continuous Learning Machine: Designing a Loop That Actually Turns
    212
  22. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    188
  23. Designing a Data Curation System Around a Vector Database
    212
  24. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    228
  25. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    646
  26. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    180
  27. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    203
  28. Understanding Data Lineage
    251
  29. System Requirements and Architecture: Intelligent Priority-Based Scheduler
    159
  30. GPU & LLM Cluster Infrastructure & Task Scheduling Architecture
    229