miniaudio/ports
Overview
These are ports of the official miniaudio examples found here.
Because we mirror the structure of the original examples, they need to be run with -file.
For example, to run the simple_playback.odin example: odin run miniaudio/simple_playback.odin -file
Code
simple_playback.odin ¶60 linesSource
1package main
2
3import "base:runtime"
4
5import "core:fmt"
6import "core:os"
7
8import ma "vendor:miniaudio"
9
10data_callback :: proc "c" (device: ^ma.device, output: rawptr, input: rawptr, frame_count: u32) {
11 decoder := (^ma.decoder)(device.pUserData)
12 if decoder == nil {
13 return
14 }
15
16 ma.decoder_read_pcm_frames(decoder, output, u64(frame_count), nil)
17}
18
19main :: proc() {
20 args := runtime.args__
21 if len(args) < 2 {
22 fmt.eprintfln("No input file.")
23 os.exit(-1)
24 }
25
26 decoder: ma.decoder
27 result := ma.decoder_init_file(args[1], nil, &decoder)
28 if result != .SUCCESS {
29 fmt.eprintfln("Could not load file: %s", args[1])
30 os.exit(-2)
31 }
32
33 device_config := ma.device_config_init(.playback)
34 device_config.playback.format = decoder.outputFormat
35 device_config.playback.channels = decoder.outputChannels
36 device_config.sampleRate = decoder.outputSampleRate
37 device_config.dataCallback = data_callback
38 device_config.pUserData = &decoder
39
40 device: ma.device
41 if ma.device_init(nil, &device_config, &device) != .SUCCESS {
42 fmt.eprintfln("Failed to open playback device.")
43 ma.decoder_uninit(&decoder)
44 os.exit(-3)
45 }
46
47 if ma.device_start(&device) != .SUCCESS {
48 fmt.eprintfln("Failed to start playback device.")
49 ma.device_uninit(&device)
50 ma.decoder_uninit(&decoder)
51 os.exit(-4)
52 }
53
54 fmt.printfln("Press Enter to quit...")
55 p: [1]byte
56 os.read(os.stdin, p[:])
57
58 ma.device_uninit(&device)
59 ma.decoder_uninit(&decoder)
60}