comparison semiconginev2/audio/resources.nim @ 1234:841e12f33c47

add: text & font rendering, not tested yet
author sam <sam@basx.dev>
date Sat, 20 Jul 2024 00:03:57 +0700
parents 27cd1c21290e
children
comparison
equal deleted inserted replaced
1233:1cf698973dca 1234:841e12f33c47
52 result.add stream.readSample(header.encoding, int(header.channels)) 52 result.add stream.readSample(header.encoding, int(header.channels))
53 53
54 {.compile: currentSourcePath.parentDir() & "/stb_vorbis.c".} 54 {.compile: currentSourcePath.parentDir() & "/stb_vorbis.c".}
55 55
56 proc stb_vorbis_decode_memory(mem: pointer, len: cint, channels: ptr cint, sample_rate: ptr cint, output: ptr ptr cshort): cint {.importc.} 56 proc stb_vorbis_decode_memory(mem: pointer, len: cint, channels: ptr cint, sample_rate: ptr cint, output: ptr ptr cshort): cint {.importc.}
57 proc free(p: pointer) {.importc.}
58 57
59 proc ReadVorbis*(stream: Stream): SoundData = 58 proc ReadVorbis*(stream: Stream): SoundData =
60 var 59 var
61 data = stream.readAll() 60 data = stream.readAll()
62 channels: cint 61 channels: cint
71 raise newException(Exception, &"Only support sample rate of {AUDIO_SAMPLE_RATE} Hz but got {sampleRate} Hz, please resample (e.g. ffmpeg -i <infile> -acodec libvorbis -ar {AUDIO_SAMPLE_RATE} <outfile>)") 70 raise newException(Exception, &"Only support sample rate of {AUDIO_SAMPLE_RATE} Hz but got {sampleRate} Hz, please resample (e.g. ffmpeg -i <infile> -acodec libvorbis -ar {AUDIO_SAMPLE_RATE} <outfile>)")
72 71
73 if channels == 2: 72 if channels == 2:
74 result.setLen(int(nSamples)) 73 result.setLen(int(nSamples))
75 copyMem(addr result[0], output, nSamples * sizeof(Sample)) 74 copyMem(addr result[0], output, nSamples * sizeof(Sample))
76 free(output) 75 nativeFree(output)
77 elif channels == 1: 76 elif channels == 1:
78 for i in 0 ..< nSamples: 77 for i in 0 ..< nSamples:
79 let value = cast[ptr UncheckedArray[int16]](output)[i] 78 let value = cast[ptr UncheckedArray[int16]](output)[i]
80 result.add [value, value] 79 result.add [value, value]
81 free(output) 80 nativeFree(output)
82 else: 81 else:
83 free(output) 82 nativeFree(output)
84 raise newException(Exception, "Only support mono and stereo audio at the moment (1 or 2 channels), but found " & $channels) 83 raise newException(Exception, "Only support mono and stereo audio at the moment (1 or 2 channels), but found " & $channels)
85 84
86 proc LoadAudio*(path: string, package = DEFAULT_PACKAGE): SoundData = 85 proc LoadAudio*(path: string, package = DEFAULT_PACKAGE): SoundData =
87 if path.splitFile().ext.toLowerAscii == ".au": 86 if path.splitFile().ext.toLowerAscii == ".au":
88 loadResource_intern(path, package = package).ReadAU() 87 loadResource_intern(path, package = package).ReadAU()