Fmx Samples: Delphi
Workaround: Supplement with third-party books (e.g., “FireMonkey Mobile Development”) and GitHub repos like FMXExpress or DelphiWorld.
A sample demonstrating the use of TMapView to display a map.
program MapViewSample;
uses
System.StartUpCopy,
FMX.Forms,
FMX.Controls,
FMX.Types,
FMX.MapView;
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Form1.MapView1.MapType := TMapType.Normal;
Application.Run;
end.
unit AudioSpectrumAnalyzer;interface
uses System.Classes, System.SysUtils, System.Threading, FMX.Forms, FMX.Types, FMX.Controls, FMX.Layouts, FMX.Memo, FMX.Objects, FMX.Media, FMX.Types3D, FMX.Controls3D, FMX.Viewport3D, FMX.Objects3D, System.Math, System.Generics.Collections;
type TAudioSpectrumAnalyzer = class(TForm) Viewport3D: TViewport3D; Camera: TCamera; Light: TLight; TimerUpdate: TTimer; MediaPlayer: TMediaPlayer; ButtonPlay: TButton; ButtonPause: TButton; ButtonOpen: TButton; OpenDialog: TOpenDialog;
procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure TimerUpdateTimer(Sender: TObject); procedure ButtonPlayClick(Sender: TObject); procedure ButtonPauseClick(Sender: TObject); procedure ButtonOpenClick(Sender: TObject);private FBars: TObjectList<TCube>; FFFTBuffer: TArray<Double>; FCanvasBuffer: TArray<Double>; FThread: TTask; FIsAnalyzing: Boolean;
procedure CreateSpectrumBars; procedure UpdateSpectrumBars; procedure AnalyzeAudioBuffer; procedure PerformFFT(const Buffer: TArray<Double>); function ApplyWindowFunction(const Buffer: TArray<Double>; WindowType: Integer): TArray<Double>;public property IsAnalyzing: Boolean read FIsAnalyzing write FIsAnalyzing; end;
var AudioSpectrumAnalyzer: TAudioSpectrumAnalyzer;
implementation
$R *.fmx
procedure TAudioSpectrumAnalyzer.FormCreate(Sender: TObject); begin Caption := '3D Audio Spectrum Analyzer - Delphi FMX Demo'; Width := 1024; Height := 768;
// Initialize 3D viewport Viewport3D.Align := TAlignLayout.Client; Viewport3D.Color := TAlphaColors.Black;
// Setup camera position for better view Camera.Position.Point := TPoint3D.Create(0, 5, 20); Camera.Target := TPoint3D.Create(0, 0, 0);
// Create spectrum visualization bars CreateSpectrumBars; delphi fmx samples
// Initialize FFT buffer SetLength(FFFTBuffer, 2048); SetLength(FCanvasBuffer, 1024);
FIsAnalyzing := False;
// Timer for smooth 60 FPS updates TimerUpdate.Interval := 16; // ~60 FPS TimerUpdate.Enabled := True; end;
procedure TAudioSpectrumAnalyzer.CreateSpectrumBars; var i: Integer; Bar: TCube; XPos: Single; begin FBars := TObjectList<TCube>.Create(True);
for i := 0 to 63 do // 64 bars for spectrum visualization begin Bar := TCube.Create(Viewport3D); Bar.Parent := Viewport3D;
// Create a gradient material for each bar Bar.MaterialSource := TMaterialSource.Create(Bar); // Position bars in a semi-circle XPos := (i - 32) * 0.15; Bar.Position.Point := TPoint3D.Create(XPos, -2, 0); Bar.Width := 0.12; Bar.Height := 0.1; Bar.Depth := 0.1; // Initial scale (height will be animated) Bar.Scale.Point := TPoint3D.Create(1, 0.1, 1); FBars.Add(Bar);end; end;
procedure TAudioSpectrumAnalyzer.ButtonOpenClick(Sender: TObject); begin if OpenDialog.Execute then begin MediaPlayer.FileName := OpenDialog.FileName; ButtonPlay.Enabled := True; end; end;
procedure TAudioSpectrumAnalyzer.ButtonPlayClick(Sender: TObject); begin MediaPlayer.Play; FIsAnalyzing := True;
// Start audio analysis in background thread FThread := TTask.Run(procedure begin while FIsAnalyzing do begin AnalyzeAudioBuffer; TThread.Queue(nil, UpdateSpectrumBars); TThread.Sleep(30); // Update every 30ms end; end); end;
procedure TAudioSpectrumAnalyzer.ButtonPauseClick(Sender: TObject); begin FIsAnalyzing := False; if Assigned(FThread) then FThread.Wait; MediaPlayer.Stop; end;
procedure TAudioSpectrumAnalyzer.AnalyzeAudioBuffer; // This simulates audio capture - in real implementation, you'd capture from MediaPlayer var i: Integer; begin // Simulate audio data with sine waves at different frequencies for i := 0 to Length(FFFTBuffer) - 1 do begin // Generate test signals (replace with actual audio capture) FFFTBuffer[i] := Sin(2 * Pi * 100 * i / 44100) * 0.5 + // 100 Hz bass Sin(2 * Pi * 440 * i / 44100) * 0.3 + // 440 Hz mid Sin(2 * Pi * 2000 * i / 44100) * 0.2; // 2 kHz treble end;
PerformFFT(FFFTBuffer); end;
procedure TAudioSpectrumAnalyzer.PerformFFT(const Buffer: TArray<Double>); // Simplified FFT for demonstration - in real app use a proper FFT library var i: Integer; begin // This is a simplified magnitude calculation // In production, use FFTPACK or similar library Workaround: Supplement with third-party books (e
// Calculate magnitudes for each frequency band for i := 0 to 63 do begin // Map to frequency bands (20Hz to 20kHz) var Magnitude := 0.0; for var j := 0 to 31 do begin var Index := i * 32 + j; if Index < Length(Buffer) then Magnitude := Magnitude + Abs(Buffer[Index]); end;
// Normalize and apply logarithmic scaling Magnitude := Magnitude / 32; Magnitude := 20 * Log10(1 + Magnitude * 100); // Store in canvas buffer with smoothing if FCanvasBuffer[i] = 0 then FCanvasBuffer[i] := Magnitude else FCanvasBuffer[i] := FCanvasBuffer[i] * 0.7 + Magnitude * 0.3; // Clamp to reasonable range if FCanvasBuffer[i] > 1 then FCanvasBuffer[i] := 1;end; end;
procedure TAudioSpectrumAnalyzer.UpdateSpectrumBars; var i: Integer; HeightScale: Single; Bar: TCube; Color: TAlphaColor; begin for i := 0 to FBars.Count - 1 do begin Bar := FBars[i];
// Map magnitude to bar height (0.1 to 3.0) HeightScale := 0.1 + FCanvasBuffer[i] * 3.0; Bar.Scale.Point := TPoint3D.Create(1, HeightScale, 1); // Color based on frequency and intensity // Low frequencies (bass) = Red, Mid = Green, High = Blue if i < 20 then // Bass Color := TAlphaColorF.Create(FCanvasBuffer[i], 0.2, 0.2, 1).ToAlphaColor else if i < 45 then // Mid Color := TAlphaColorF.Create(0.2, FCanvasBuffer[i], 0.3, 1).ToAlphaColor else // Treble Color := TAlphaColorF.Create(0.3, 0.2, FCanvasBuffer[i], 1).ToAlphaColor; // Add pulse effect on peaks if FCanvasBuffer[i] > 0.8 then Color := TAlphaColorF.Create(1, 1, 1, 1).ToAlphaColor; Bar.Material := TColorMaterial.Create(Color); // Add rotation effect for extreme frequencies if FCanvasBuffer[i] > 0.9 then Bar.RotateAngle.Point := TPoint3D.Create(0, FCanvasBuffer[i] * 360, 0);end; end;
procedure TAudioSpectrumAnalyzer.FormDestroy(Sender: TObject); begin FIsAnalyzing := False; if Assigned(FThread) then FThread.Wait; FBars.Free; end;
procedure TAudioSpectrumAnalyzer.TimerUpdateTimer(Sender: TObject); begin // Add subtle camera movement for dynamic effect Camera.RotateAngle.Point := TPoint3D.Create( Sin(Now * 2) * 5, // Gentle pan Cos(Now) * 3, // Gentle tilt 0 );
Viewport3D.Render; end;
end.
Many UI samples rely on a .style file. If you get black screens or invisible buttons, locate the TStyleBook component and verify the ResourceName property.
Don't go digging through Google blindly. Here are the three best repositories:
1. The RAD Studio Installation Folder This is your bread and butter. You
Exploring FireMonkey: A Guide to the Best Delphi FMX Samples A sample demonstrating the use of TMapView to
Delphi’s FireMonkey (FMX) framework is the powerhouse behind some of the most versatile cross-platform applications today. Whether you’re targeting Windows, macOS, iOS, Android, or Linux, the best way to master FMX is by diving into high-quality code samples.
Here is a curated look at the essential Delphi FMX samples and where to find them to kickstart your next project. 1. The Official RAD Studio Demo Repositories
The most reliable starting point is the official collection of demos provided by Embarcadero. These are updated for each major release, such as the RAD Studio 12 Demos on GitHub.
MultiView Demo: Learn how to create responsive navigation drawers that adapt perfectly to both mobile and desktop screens.
Photo Editor Demo: A great example of using built-in FMX effects and camera integration to build a functional utility app.
3D Objects & Animation: Explore FMX.FirstApp3D to see how to use components like TCamera, TCube, and TFloatAnimation to build hardware-accelerated 3D scenes. 2. Massive Community Collections: FMXExpress
For those who want a "one-stop-shop," the FMXExpress Cross-Platform Samples project is legendary. It contains over 100 demos that use a single codebase for all platforms.
Connectivity: Samples for Bluetooth, REST clients (using TFDMemTable), and local notifications.
UI Components: Examples of modern mobile patterns like "Pull to Refresh," "Bottom Sheets," and "Drawer Menus".
Real-World Apps: Check out the BubbleChatApp or the ToDoList demo for end-to-end application architecture. 3. Specialized Game & UI Snippets
Delphi isn't just for business; it's surprisingly capable of 2D and 3D game development. FMX.MultiViewDemo Sample - RAD Studio Code Examples
A sample demonstrating the use of TListView to display a list of items.
program ListViewSample;
uses
System.StartUpCopy,
FMX.Forms,
FMX.Controls,
FMX.Types,
FMX.ListView;
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Form1.ListView1.Items.Add('Item 1');
Form1.ListView1.Items.Add('Item 2');
Form1.ListView1.Items.Add('Item 3');
Application.Run;
end.
Many FMX samples contain conditional defines:
$IFDEF IOS
// iOS-specific camera access
$ENDIF
$IFDEF ANDROID
// Android-specific intent handling
$ENDIF
Replace these with your own service interfaces or use the IFMXPlatformServices pattern.