London Market 33 · Make an order chime

Market Day London · Lesson 33 of 50 · Intermediate to advanced · About 35 min

The order bag and sound should tell the same story. Customers get a confirmation even when the game is muted.

Watercolour of a dry London market lane lined with bread, produce and flower stalls.
What you’ll make Create two short audio cues and attach them to visible order feedback.

Download the projects from the course page. In Godot’s Project Manager, choose Import, select project.godot, and use Run Project (F5). Start from checkpoints/32 when the lesson builds on prior work, or copy the checkpoints/33 folder to try this lesson on its own. Read the file names in the steps before editing.

Sample. Write values for a sound wave. → Fade. Soften both edges. → Announce. Show the order result. → Play. Use sound when enabled.
Sample → Fade → Announce → Play
Godot checkpoint window for Make an order chime
From the running Godot checkpoint.

Build it step by step

Language and tools in this lesson AudioStreamWAV, PackedByteArray, encode_s16, sin, AudioStreamPlayer.

  1. Import the editable checkpoint

    In the Godot 4.7.2 Project Manager, select Import and open the project.godot in this lesson folder. Open main.gd in the FileSystem dock. Press F5 to run the market and F8 to stop it before editing.

    Why it works This checkpoint already has a Control root with its script attached, so you can compare your edits to a working market scene.

  2. Link pitches to outcomes

    Click both delivery buttons and listen. In _ready find 660.0 and 330.0. Try 520.0 for the success tone and compare.

    good = make_chime(660.0)

    Why it works Hertz counts cycles per second. A higher number gives a higher note but the order outcome still comes from accepted.

  3. Reserve two bytes per sample

    Read sample_count and audio.resize. Calculate 22,050 times 0.20, then double the answer.

    audio.resize(sample_count * 2)

    Why it works One signed 16-bit sample needs two bytes. The buffer stores 4,410 samples in 8,820 bytes.

  4. Shape the start and end

    Read fade in the loop. Compare the fade at time zero, near the middle and close to the final sample.

    var fade := minf(seconds / 0.02, (1.0 - seconds / DURATION) * 4.0)

    Why it works The minimum of the opening ramp and closing ramp keeps the wave near zero at either edge. This reduces audible clicks.

  5. Show the message before checking mute

    Toggle Audio off and press both delivery buttons again. Follow status.text then the early return.

    status.text = "✓ Order handed over" if accepted else "↻ The order still needs an item"

    Why it works A player who cannot hear the audio still receives the visible order result.

  6. Stop an old sound on mute

    Switch audio on, press Deliver matching order, then switch it off. Inspect player.stop in toggle_audio.

    player.stop()

    Why it works Stopping clears the previous cue right away while leaving the on-screen text available.

Watch the values change

  1. Hand over matching order accepted is true.
    What you see. A tick appears and the high cue plays.
  2. Try incomplete order accepted is false.
    What you see. A retry message appears with the low cue.
  3. Mute then hand over The status changes before the early return.
    What you see. Only the visual confirmation appears.

Predict before you run What feedback survives when the player turns audio off?

Reveal the answer

The confirmation symbol and text still change with the order.

Complete checkpoint

A visible result and two distinct original tones. Muting keeps delivery feedback on screen.

main.gd

extends Control

const RATE := 22050
const DURATION := 0.20
var player: AudioStreamPlayer
var good: AudioStreamWAV
var retry: AudioStreamWAV
var status: Label
var mute: Button

func _ready() -> void:
	heading("Make an order chime", "AUDIO")
	label("Compare a completed bundle with an order that still needs an item.", Vector2(44, 119), 18, MINT)
	status = label("Waiting for a delivery", Vector2(44, 365), 23)
	player = AudioStreamPlayer.new()
	player.volume_db = -15.0
	add_child(player)
	good = make_chime(660.0)
	retry = make_chime(330.0)
	button("Deliver matching order", Vector2(44, 217), deliver.bind(true), 214)
	button("Try incomplete order", Vector2(274, 217), deliver.bind(false), 211)
	mute = button("Audio: on", Vector2(44, 285), toggle_audio)
	mute.toggle_mode = true

func make_chime(hertz: float) -> AudioStreamWAV:
	var sample_count := int(RATE * DURATION)
	var audio := PackedByteArray()
	audio.resize(sample_count * 2)
	for index in range(sample_count):
		var seconds := float(index) / RATE
		var fade := minf(seconds / 0.02, (1.0 - seconds / DURATION) * 4.0)
		var amplitude := int(sin(TAU * hertz * seconds) * fade * 10000.0)
		audio.encode_s16(index * 2, amplitude)
	var stream := AudioStreamWAV.new()
	stream.format = AudioStreamWAV.FORMAT_16_BITS
	stream.mix_rate = RATE
	stream.data = audio
	return stream

func deliver(accepted: bool) -> void:
	status.text = "✓ Order handed over" if accepted else "↻ The order still needs an item"
	if mute.button_pressed:
		return
	player.stream = good if accepted else retry
	player.play()

func toggle_audio() -> void:
	mute.text = "Audio: off" if mute.button_pressed else "Audio: on"
	if mute.button_pressed:
		player.stop()

func _draw() -> void:
	base()

const INK := Color("#173353")
const PAPER := Color("#fff4db")
const MINT := Color("#a8e0c1")
const GOLD := Color("#ffd291")

func heading(caption: String, topic: String) -> void:
	label("MARKET DAY LONDON     /     " + topic, Vector2(44, 36), 15, GOLD)
	label(caption, Vector2(44, 66), 31)

func label(words: String, at: Vector2, pixels: int = 20, ink: Color = PAPER) -> Label:
	var item := Label.new()
	item.text = words
	item.position = at
	item.add_theme_font_size_override("font_size", pixels)
	item.add_theme_color_override("font_color", ink)
	add_child(item)
	return item

func button(words: String, at: Vector2, action: Callable, width: float = 164.0) -> Button:
	var control := Button.new()
	control.text = words
	control.position = at
	control.custom_minimum_size = Vector2(width, 43)
	control.pressed.connect(action)
	add_child(control)
	return control

func base(colour: Color = Color("#193652")) -> void:
	draw_rect(Rect2(Vector2.ZERO, size), Color("#11263e"))
	draw_rect(Rect2(Vector2(22, 20), size - Vector2(44, 40)), colour)

GDScript line by line

Follow the line numbers, including the indent at the start of each line. Find the code the game will run first, then look for the function that reacts when you tap or change something.

Read main.gd line by line
  1. extends ControlStarts with Godot's Control type. This script gets the abilities of that type and adds the game's own rules.
  2. const RATE := 22050RATE is the number of audio samples recorded into the wave every second.
  3. const DURATION := 0.20Creates a constant named DURATION. The expression reads 0.20. Godot infers the type from the first value.
  4. var player: AudioStreamPlayerDeclares player for later use. A colon followed by a type says which kind of value belongs here.
  5. var good: AudioStreamWAVDeclares good for later use. A colon followed by a type says which kind of value belongs here.
  6. var retry: AudioStreamWAVDeclares retry for later use. A colon followed by a type says which kind of value belongs here.
  7. var status: LabelDeclares status for later use. A colon followed by a type says which kind of value belongs here.
  8. var mute: ButtonDeclares mute for later use. A colon followed by a type says which kind of value belongs here.
  9. func _ready() -> void:Godot calls this once after the scene and its children are ready. Use it to set up the room and connect controls.
  10. heading("Make an order chime", "AUDIO")Calls heading with the values in its parentheses.
  11. label("Compare a completed bundle with an order that still needs an item.", Vector2(44, 119), 18, MINT)Calls label with the values in its parentheses.
  12. status = label("Waiting for a delivery", Vector2(44, 365), 23)Stores the new value in status. The expression calls label with the values in its parentheses.
  13. player = AudioStreamPlayer.new()Stores the new value in player. The expression creates a new AudioStreamPlayer object.
  14. player.volume_db = -15.0A lower decibel value makes the delivery button play a gentler sound.
  15. add_child(player)Adds the new node to the scene tree so that Godot can run and draw it.
  16. good = make_chime(660.0)660 hertz produces the higher completed-order chime. The incomplete order uses half that frequency.
  17. retry = make_chime(330.0)Stores the new value in retry. The expression calls make_chime with the values in its parentheses.
  18. button("Deliver matching order", Vector2(44, 217), deliver.bind(true), 214)Calls button with the values in its parentheses.
  19. button("Try incomplete order", Vector2(274, 217), deliver.bind(false), 211)Calls button with the values in its parentheses.
  20. mute = button("Audio: on", Vector2(44, 285), toggle_audio)Stores the new value in mute. The expression calls button with the values in its parentheses.
  21. mute.toggle_mode = trueToggle mode makes the mute button remember a pressed or unpressed state.
  22. func make_chime(hertz: float) -> AudioStreamWAV:Defines make chime, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect.
  23. var sample_count := int(RATE * DURATION)Multiply samples per second by the duration to get the sample count, then convert it to an integer for the loop.
  24. var audio := PackedByteArray()Creates a variable named audio. The expression calls PackedByteArray with the values in its parentheses. Godot infers the type from the first value.
  25. audio.resize(sample_count * 2)Reserve two bytes per sample. One 16-bit sample needs sixteen bits or two bytes.
  26. for index in range(sample_count):Repeats the indented instructions for each value in range(sample_count).
  27. var seconds := float(index) / RATEDividing a sample’s index by the sample rate gives the fraction of a second it represents.
  28. var fade := minf(seconds / 0.02, (1.0 - seconds / DURATION) * 4.0)Use the smaller of the fade-in and fade-out amounts to soften the two ends of the wave.
  29. var amplitude := int(sin(TAU * hertz * seconds) * fade * 10000.0)sin() moves smoothly above and below zero. TAU represents one full cycle and hertz sets how often it repeats.
  30. audio.encode_s16(index * 2, amplitude)Write the sample at its byte position. Multiplying the index by two leaves room for every sample.
  31. var stream := AudioStreamWAV.new()Creates a variable named stream. The expression creates a new AudioStreamWAV object. Godot infers the type from the first value.
  32. stream.format = AudioStreamWAV.FORMAT_16_BITSStores the new value in stream.format. The expression reads AudioStreamWAV.FORMAT_16_BITS.
  33. stream.mix_rate = RATEMatch the playback rate to the rate used to calculate the samples to preserve the intended timing and pitch.
  34. stream.data = audioStores the new value in stream.data. The expression reads audio.
  35. return streamFinishes the function and hands its result back to the caller. The expression reads stream.
  36. func deliver(accepted: bool) -> void:Defines deliver, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect.
  37. status.text = "✓ Order handed over" if accepted else "↻ The order still needs an item"Update the words first so the delivery result stays visible with audio switched off.
  38. if mute.button_pressed:Checks mute.button_pressed. The indented instructions below run only if this test passes.
  39. returnFinishes the function right here. Any later instructions in this function are skipped.
  40. player.stream = good if accepted else retryStores the new value in player.stream. The expression chooses one of two values by checking the condition in the middle.
  41. player.play()Starts this sound or animation.
  42. func toggle_audio() -> void:Defines toggle audio, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect.
  43. mute.text = "Audio: off" if mute.button_pressed else "Audio: on"Updates mute.text, which controls the words a player sees. The expression chooses one of two values by checking the condition in the middle.
  44. if mute.button_pressed:If the toggle has just muted audio, stop any chime that is already in progress.
  45. player.stop()Stops this sound, timer or animation.
  46. func _draw() -> void:Godot calls this when the node needs drawing. The instructions inside paint the current state.
  47. base()Calls base with the values in its parentheses.
  48. const INK := Color("#173353")Creates a constant named INK. The expression builds a colour from these values. Godot infers the type from the first value.
  49. const PAPER := Color("#fff4db")Creates a constant named PAPER. The expression builds a colour from these values. Godot infers the type from the first value.
  50. const MINT := Color("#a8e0c1")Creates a constant named MINT. The expression builds a colour from these values. Godot infers the type from the first value.
  51. const GOLD := Color("#ffd291")Creates a constant named GOLD. The expression builds a colour from these values. Godot infers the type from the first value.
  52. func heading(caption: String, topic: String) -> void:Defines heading, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect.
  53. label("MARKET DAY LONDON / " + topic, Vector2(44, 36), 15, GOLD)Calls label with the values in its parentheses.
  54. label(caption, Vector2(44, 66), 31)Calls label with the values in its parentheses.
  55. func label(words: String, at: Vector2, pixels: int = 20, ink: Color = PAPER) -> Label:Defines label, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect.
  56. var item := Label.new()Creates a variable named item. The expression creates a new Label object. Godot infers the type from the first value.
  57. item.text = wordsUpdates item.text, which controls the words a player sees. The expression reads words.
  58. item.position = atUpdates item.position, which controls the node's local x and y location. The expression reads at.
  59. item.add_theme_font_size_override("font_size", pixels)Calls item.add_theme_font_size_override with the values in its parentheses.
  60. item.add_theme_color_override("font_color", ink)Calls item.add_theme_color_override with the values in its parentheses.
  61. add_child(item)Adds the new node to the scene tree so that Godot can run and draw it.
  62. return itemFinishes the function and hands its result back to the caller. The expression reads item.
  63. func button(words: String, at: Vector2, action: Callable, width: float = 164.0) -> Button:Defines button, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect.
  64. var control := Button.new()Creates a variable named control. The expression creates a new Button object. Godot infers the type from the first value.
  65. control.text = wordsUpdates control.text, which controls the words a player sees. The expression reads words.
  66. control.position = atUpdates control.position, which controls the node's local x and y location. The expression reads at.
  67. control.custom_minimum_size = Vector2(width, 43)Updates control.custom_minimum_size, which controls the smallest space the layout should reserve for this control. The expression packs x and y into one value.
  68. control.pressed.connect(action)Links a signal to the function that should run when the event happens.
  69. add_child(control)Adds the new node to the scene tree so that Godot can run and draw it.
  70. return controlFinishes the function and hands its result back to the caller. The expression reads control.
  71. func base(colour: Color = Color("#193652")) -> void:Defines base, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect.
  72. draw_rect(Rect2(Vector2.ZERO, size), Color("#11263e"))Paints a rectangle at the given position and size.
  73. draw_rect(Rect2(Vector2(22, 20), size - Vector2(44, 40)), colour)Paints a rectangle at the given position and size.

Find and fix a bug

If the player cannot hear a cue with audio on, check that the AudioStreamPlayer is in the scene and its stream is assigned before play.

Try a change

Ages 10–12 can make two new pitches. Ages 13–16 can add a volume slider with a labelled range and a comfortable upper limit.