[HN Gopher] Playing audio files in a Pi Pico without a DAC
       ___________________________________________________________________
        
       Playing audio files in a Pi Pico without a DAC
        
       Author : surprisetalk
       Score  : 77 points
       Date   : 2024-03-06 12:12 UTC (2 days ago)
        
 (HTM) web link (antirez.com)
 (TXT) w3m dump (antirez.com)
        
       | Nihilartikel wrote:
       | The little 8 bit atmel attiny85 has a great fastpwm feature. It
       | can get almost 20khz output,with 8 bit duty cycle control. This
       | is plenty to make a multichannel chip tune soft synth. Sounds
       | good just hooking the pwm pin directly to an earbud.
        
       | tempodox wrote:
       | Meinolf Schneider in his Esprit and Oxyd games on the Atari ST
       | used some creative sound chip programming in the 1980s to play
       | arbitrary samples, also without a DAC. I found the source code
       | for that published somewhere years later.
       | 
       | https://www.myabandonware.com/search/q/oxyd
        
         | graemep wrote:
         | I remember playing tunes using the serial output of a Grundy
         | Newbrain. Not exactly hifibut fun.
        
       | robmccoll wrote:
       | Wouldn't you typically want to run that output through an RC (low
       | pass resistor-capacitor) filter to average out the voltage and
       | eliminate high frequency PWM noise? Is that built into the pico?
        
         | praash wrote:
         | This would be required for a true DAC, but not here:
         | 
         | - A physical speaker already acts as a filter due to inertia
         | 
         | - The PWM frequency and its square harmonics sit far beyond the
         | human hearing range
        
         | crote wrote:
         | You'd normally do that, yes, but it's not strictly required in
         | all applications. The speaker itself might already act as a
         | low-pass filter simply because it can't physically react fast
         | enough for the high-frequency part.
        
         | duped wrote:
         | Well then you wouldn't be playing audio without a DAC, would
         | you?
        
           | jalk wrote:
           | How would you consider an RC filter a DAC? there is nothing
           | digital about it
        
             | duped wrote:
             | It's a reconstruction filter, the simplest DAC you could
             | build.
             | 
             | Most of the digital parts of DAC chips are not there for
             | conversion, but getting data ready for conversion through a
             | reconstruction filter. You don't need that if you're
             | driving a PWM signal through a cap to integrate it.
        
       | ale42 wrote:
       | circuitpython uses PWM to do the same, see for example
       | https://learn.adafruit.com/circuitpython-essentials/circuitp....
       | I tried connecting a speaker with just a single MOSFET as a
       | digital amplifier, and there was nothing to hear from the
       | original PWM signal, it's probably completely out of the
       | speaker's output range (didn't check though).
        
       | danhor wrote:
       | Using PWM to play audio is flawed (but usable for many
       | applications), other approaches such a sigma-delta will lead to
       | much better results, but with a lot more effort.
       | https://github.com/tierneytim/Pico-USB-audio seems like a
       | reasonable implementation at first glance.
        
         | Kirby64 wrote:
         | What do you mean? Class D amps are essentially PWM driven
         | amplifiers. Essentially any speaker in existence offers enough
         | low pass filtering to be directly wired to the amplifier, as
         | well.
        
           | namibj wrote:
           | They still do noise shaping.
        
             | Kirby64 wrote:
             | Good ones do all sorts of tricks, including closed loop
             | control and special pwm pulse trains... that doesn't mean
             | pwm is inherently flawed though.
        
           | squarefoot wrote:
           | Also, there's a world of difference between PWM out of a CPU
           | running a non realtime multitasking OS with continuous
           | scheduling and varying timings and the same PWM produced by a
           | dedicated chip that does nothing else.
        
       | 127 wrote:
       | Nice stereo I2S 16-bit 48kHz is like 1 USD chip. Plus I2S is very
       | efficient to implement with the RP2040 PIO hardware. You can even
       | use the blocking write call to time the signal without any timers
       | or callbacks necessary.
        
       | retrac wrote:
       | I did something similar on a Raspberry Pi using the GPIO pins one
       | afternoon when bored. I made a 4-bit R2R DAC [1] using typical 5%
       | tolerance resistors and fed it directly into a small speaker. I
       | shrunk a 16 bit audio file down to 4 bits and then toggled the
       | GPIO pins in a loop in Python from Linux userspace around 40 kHz.
       | The result was easily intelligible speech, and easily
       | recognizable, if not exactly pleasant, music. I was surprised how
       | good the results were considering it was made of literally
       | nothing but wire. It was quite noisy, which I assume was from the
       | clock jitter running with an inexactly timed update loop, and the
       | zero filtering.
       | 
       | [1]
       | https://en.wikipedia.org/wiki/Resistor_ladder#R%E2%80%932R_r...
        
         | TickleSteve wrote:
         | If you're doing that from user space, the noise will be from
         | scheduling delays as Linux context-switches your process to run
         | other stuff. You can minimise that type of effect thru use of
         | cpu-affinity but not get rid of it completely.
        
       | klinquist wrote:
       | As brilliant as Antirez is, he can't get his own valid SSL cert?
       | :)
        
         | westhanover wrote:
         | Or he is brilliant enough to know you don't need one on your
         | blog. I really don't care if someone messes with your traffic
         | to my blog!
        
           | tambourine_man wrote:
           | Agreed on principle, but the "not secure" warning in the url
           | bar is an annoyance I cannot stand.
        
             | OJFord wrote:
             | Well the full-page insecure advanced continue with http is
             | even worse. I (we) understand there's no real reason to
             | need it, but if you're serving the general public (as isn't
             | the case here) you absolutely need TLS whatever you're
             | doing in 2024 IMO. Personally if I were blogging even for
             | this audience I'd want to avoid that friction, but I think
             | it's not like antirez doesn't know about it, I assumed it's
             | a sort of protest.
        
           | o11c wrote:
           | Not using HTTPS means that anybody can inject malware for
           | visitors to your website.
           | 
           | There are no websites that don't need SSL.
        
         | tambourine_man wrote:
         | Or a better way to navigate the gazillion posts in his site.
         | There are no categories, not even a way to list all posts by
         | title only that I could find.
        
       | polpo wrote:
       | I'm going to push back on the comment in the article about the
       | build system being overkill. While "tiny" is a relative term, not
       | all projects built for the RP2040 are tiny - my PicoGUS project
       | relies on several external libraries and being able to bring them
       | in via CMake is pretty great. Also while the Pico itself is fixed
       | hardware, the SDK targets many other boards built on the RP2040.
       | The Raspberry Pi team definitely have things architected for
       | future versions of the microcontroller as well as on-host testing
       | of the SDK.
        
         | kaycebasques wrote:
         | We (the Pigweed team) are big fans of the RP2040/Pico
         | ecosystem. We built out a pretty deep GN-based build system for
         | it; check out the source code [1] for our Gameboy-style
         | electronic badge Kudzu [2] for an example. We're also
         | prototyping Bazel integrations (since Bazel is our primary
         | build system [3] going forward). Pigweed might be overkill [4]
         | for small hobby projects but we think it might be pretty nice
         | for big/complex projects built on top of RP2040. I know you
         | said you're happy with CMake (and we too think CMake is great;
         | Pigweed also has CMake support), I'm just excited that there
         | are other build system options and figured others might be
         | interested to hear that other options exist.
         | 
         | [1]
         | https://pigweed.googlesource.com/pigweed/kudzu/+/refs/heads/...
         | 
         | [2] https://pigweed.dev/docs/blog/01-kudzu.html
         | 
         | [3] https://pigweed.dev/seed/0111-build-systems.html
         | 
         | [4] https://pigweed.dev/docs/mission.html
        
           | doublepg23 wrote:
           | Is this a Google project? I didn't know google source code
           | hosting was still a thing.
        
             | kaycebasques wrote:
             | Yep, it's a Google project. We've also got a GitHub mirror:
             | https://github.com/google/pigweed
        
         | colinsane wrote:
         | i don't think the build system is overkill. but it's the same
         | phenomenon as docker: if you grew up with it then it's easy to
         | use and solves all your problems easily and you've probably
         | forgotten that it was ever difficult. but if not, then it's
         | this arcane monolith that you've no hope of actually
         | _understanding_. this applies to every general-purpose build
         | system.
        
       | isoprophlex wrote:
       | > How can be sure that it matches the MicroPython speed? well,
       | indeed it is not a perfect match, so I added "x=1" statements to
       | delay it a bit to kinda match the pitch that looked correct.
       | 
       | absolutelyharam.jpeg
        
       | Nav_Panel wrote:
       | Neat project but how's it sound? I want a recording!
        
         | OJFord wrote:
         | It's difficult to do that though isn't it, because it's
         | inherently then also a function of the recording and your
         | playback equipment.
         | 
         | Much like you can't expect a (useful) recording in a speaker
         | review.
         | 
         | It'd be a bit more reasonable here I suppose because it's
         | almost certainly going to be the weakest link, so everything
         | else is to some extent preserving characteristics of it.
        
           | Nav_Panel wrote:
           | My expectation is that the PWM output would sound
           | characteristic / interesting enough that it would be worth
           | hearing a recording -- but maybe I'm setting myself up to be
           | surprised if it has a reasonable amount of fidelity.
        
       | londons_explore wrote:
       | I assume there is probably some way to hook up the dma hardware
       | and gpio state machine to get perfect timing of delta sigma
       | modulated audio with no involvement of the CPU.
        
       | qrv3w wrote:
       | > that we converted the file to 8 bit audio
       | 
       | this actually works surprisingly well!
       | 
       | I did the same thing for a device I made based on the Pi Pico[1],
       | where audio is processed every sample and it stores a bunch of
       | wav files in the flash that can be mangled together. the flash is
       | fast enough that you can jump between samples really quickly to
       | get cool "tunneling" effects that are surprisingly good sounding
       | for such a tiny + cheap little chip.
       | 
       | [1]: https://pikocore.com
        
         | tomcam wrote:
         | Wow. Shockingly good-sounding demo on home page at
         | https://infinitedigits.co/wares/pikocore/
        
       | United857 wrote:
       | I'm dating myself here but I remember similar techniques back in
       | the original IBM PC days to play sampled audio through the built-
       | in PC speaker: https://en.wikipedia.org/wiki/RealSound
        
         | ilaksh wrote:
         | I remember finding some little schematic for a DAC and getting
         | the parts at Radio Shack and wiring it up so I could connect
         | the PC to a stereo and play tracker music or something. At
         | least I think it was a DAC. I'm guessing this was before we got
         | the SoundBlaster and Silpheed for Christmas. That was a fun
         | Christmas. Maybe early 90s?
        
           | duskwuff wrote:
           | Was it attached to the parallel port? That was a fairly
           | common design, e.g.
           | https://en.wikipedia.org/wiki/Covox_Speech_Thing
        
             | ilaksh wrote:
             | Yes probably it was a DIY version of that.
        
         | rzzzt wrote:
         | Magic Mushroom commercial song on a floppy, anyone?
        
         | sowbug wrote:
         | The Apple II had a one-bit DAC!
         | 
         | Accessing address 0xC030 (PEEK(-16336) or STA $C030) would
         | cause the machine's internal speaker to click. I assume it was
         | the result of the voltage to the speaker toggling from digital
         | on to digital off.
         | 
         | POKE(-16336) was quieter. I figured it was because it was
         | implemented as a quick read-then-write instruction sequence,
         | too fast for the speaker diaphragm to move end-to-end
         | (effectively a quieter sound). Assembly behavior was different.
         | 
         | In spite of its simplicity, there were games with amazing PWM
         | sound effects (crude by today's standards, but magical in the
         | early 1980s). The software company Muse even produced a sort of
         | speech synthesizer that played sampled words, like an audio
         | version of a ransom note. It sounded wonderfully awful.
        
       | eldruin wrote:
       | A couple of years ago I wrote a MIDI player with a bluepill board
       | (STM32F1 microcontroller) and a waveform generator (AD9833) in
       | Rust. It was actually quite simple and a lot of fun to write (and
       | hear!).
       | 
       | Video: [1]
       | 
       | Source code: [2]
       | 
       | [1]: https://blog.eldruin.com/ad983x-waveform-generator-dds-
       | drive...
       | 
       | [2]: https://github.com/eldruin/driver-
       | examples/blob/master/stm32...
        
       | kozak wrote:
       | So is this essentially DSD with a very low sampling rate?
        
         | aidenn0 wrote:
         | Not quite; DSD is pulse density modulation (PDM). This is using
         | pulse width modulation (PWM). The basic idea is the same
         | though.
         | 
         | Note that Class D amplifiers can use either PDM or PWM, though
         | the former is less common at least partly due to (now expired)
         | patents.
        
       ___________________________________________________________________
       (page generated 2024-03-08 23:00 UTC)