Shop Learn
simpleSound - A simple to use sound engine for the P2 — Parallax Forums

simpleSound - A simple to use sound engine for the P2

I thought that I should give simpleSound it's own thread instead of "multiplexing" it in the reSound thread. :lol: Yes, it has reSound as the backend and it was meant to be a stepping stone to something much more advanced. But after thinking about it some more, I came to realize that simpleSound is good enough for a lot of things. I also realized that my time is limited atm, so I will not make that advanced sound engine for a while anyway. Simply put It deserves its own thread! :sunglasses:

Here is the latest version that compiles fine with both Propeller Tool and FlexSpin

Technical notes:

  • It adapts its internal mixing frequency to whatever system clock you would want to run at, so it always sounds right while maximizing the sound quality (try changing _clkfreq)
  • It has got 12 sound channels by default, but you can have up to 64 by changing some constants
  • 4 sound channels are dedicated to play back Protracker Modules
  • 8 sound channels (default setting) is dedicated to sound effects
  • You can play s16, u16, s8, u8, little or big endianess samples, configurable per channel
  • You can play looped samples
  • You can play user defined waveforms (small looped samples)
  • You can change volume, frequency, panning in realtime individually per sound channel
  • It handles almost all available Protracker modules and Protracker commands
  • You can pollTick() the music routine for asynchronous music processing
  • Or you can do a tick() for synchronous processing locked to the main loop (needs to be 50 Hz for like 99% of available music modules)
  • It runs the heavy sound processing (using the reSound driver) in a dedicated cog
  • The music routine and the thin API/abstraction layer to trigger reSound sample playbacks runs in the calling COG and leaves most of the cycle free for other things
«134

Comments

  • Ahle2Ahle2 Posts: 1,149
    edited 2021-02-09 10:31
  • Ahle2Ahle2 Posts: 1,149

    This...

    is ALL you need as a user to get it to play that well known piece of music

  • pik33pik33 Posts: 942

    And you also did a module player !!! Wow !

  • cgraceycgracey Posts: 13,406

    Ahle, any chance you could make a YouTube video just to demonstrate the sound capability? I mean, something one could just click on to get an idea of what we're talking about here?

    It doesn't have to explain anything, but just give a sound sample.

    I think the sound quality would be very good, way beyond people's normal 8-bit expectations.

  • pik33pik33 Posts: 942

    I think the sound quality would be very good, way beyond people's normal 8-bit expectations.

    And then destroyed by youtube 128 kbps.

    Yes, the audio quality is good. First what I did after receiving an eval board (today, 5 hours ago) was SIDCog. The audio quality was very good.

    In a few moments I will try the object from this topic with some of my favourite mod files :) using much better audio devices than I did at the university with SIDCog.

  • RaymanRayman Posts: 11,842
    edited 2021-02-08 18:32

    Would this be a good way to add sound to games?

    Nevermind. I see from the filenames and the code that this is meant for games. Great!

  • ColeyColey Posts: 1,013

    Brilliant!

  • RaymanRayman Posts: 11,842

    I just tried it out. Sounds good! This looks to be a great way to add sound to games.

    Only took me a sec to figure out that the default pin settings were clobbering my VGA output because I have the A/V board on basepin 0.
    Anyway, for anyone using the A/V board, here's how to set the pins:

    CON
      LEFT_PIN     = vga_base+6
      RIGHT_PIN    = vga_base+7
    
  • RaymanRayman Posts: 11,842

    Example uses a fair amount of memory, 140 kB. But, 109 kB of that looks like the embedded music and sound files.

  • RaymanRayman Posts: 11,842

    Would it be difficult to have this play directly from the flash chip instead of HUB ram?

  • @pik33 said:
    And then destroyed by youtube 128 kbps.

    Mind that YouTube uses the Opus codec these days, which at 128 kbps VBR is considered transparent. One just has to make sure to upload the video with FLAC audio to avoid a re-encode.

  • pik33pik33 Posts: 942
    edited 2021-02-08 19:22

    Now testing a module player. Made some configuration changes as aliases in mod files were way too audible :(

    NR_SFX_CHANNELS = 4 Edit: Tried 0 here.. the module played.... strange.
    _clkfreq = 370_000_000. Tried 380 but it was not 100% stable
    reSound.init(clkfreq, mixingFreq, true, tracker.NR_CHANNELS + NR_SFX_CHANNELS, leftPin, rightPin, reSound.C_NOFX, false, @resoundHubData)

    Much better. I also reduced separation to 8192


    Why can't I use [b] or [code] now?

  • pik33pik33 Posts: 942
    edited 2021-02-08 19:31

    @Rayman said:
    Would it be difficult to have this play directly from the flash chip instead of HUB ram?

    If there is a possibility to fast, random read bytes from the flash, then yes. A module is a header, pattern list and then a set of samples. You have to play and mix samples according to the pattern so instead read from the hub you have to read trom the flash.

    If you have to read a full sector, this can make troubles as we need random access to all of the bytes of the sample bank. Or if a byte read is more than (several) microsecond(s) long, this can slow down the mixing procedures, lowering the possible sample rate=adding aliases.

  • Ahle2Ahle2 Posts: 1,149

    @pik33

    That constants only changes the number of channels usel by sound effects. 4 are always dedicated to music. In the default config with 12 channels and a sys clock of 252 MHz you get a mixing rate of around 200 kHz. You should not be able to hear any noticable aliasing from reSound itself. On the other hand, many modules uses very low quality samples and because there is no interpolation going on you will hear the that inherant distortion from the samples themselves. Most module players on modern OS adds interpolation and muffles the samples, but that is totally wrong and not how it is meant to sound.

  • pik33pik33 Posts: 942
    edited 2021-02-08 19:48

    The difference between default and modified config is audible for me. The best way to do a module player will be doing something like a real Paula did: a variable sample rate. This cannot be done on dac+pwm, the frequency, about 1.25 MHz is way too low, but maybe dac+noise can do? I still have a lot to learn.

  • RaymanRayman Posts: 11,842
    edited 2021-02-08 20:01

    Does the P2 clock have to be at 252 MHz for this to work?

  • pik33pik33 Posts: 942
    edited 2021-02-08 20:38

    @Rayman said:
    Does the P2 clock have to be at 252 MHz for this to work?

    No, as I wrote above, I am running this at 370 MHz now

  • Ahle2Ahle2 Posts: 1,149

    @Rayman
    Did you read the technical notes? :wink:
    (You could change the system clock and it will adapt all processing)

    @pik33
    I can hear it too under certain circumstances, but it is quite good at default settings. My wavplayer object (that is in the works) relies on reSound as well. But I always configure reSound to mix at multiples of the files sample rate to have aliasing free sound mixing. When I mix multiple simultaneous audio streams of different sample rates at the same time, I try to choose a mixing rate that produce as little aliasing as possible for the composit signal.

    The paula in the Amiga actually runs at half the system clock at aprox 3.55 MHz. It divides that clock per audio channel into cycle periods so the result is always alaising free. It could be done on the P2 as well (Paula would be easy to emulate) by running at the exact same clock rate. But you will have to get rid of the phase accumlators and do it the same way as the Amiga. I think the noise mode is up to it.

    The reSound driver is NOT a Paula emulator and will never be, but it can be used as a substitute for one and most people without your picky ears wouldn't notice. reSound is a general purpose sound driver and mixer; Similar to what is found in a desktop OS. It even triggers interrupt requests for each input that is configured to do double buffered audio. So you have your callbacks as well. It will produce alasing free sound if configured to run at multiples of the played samples rate.

  • pik33pik33 Posts: 942

    Yes, the reSound is a heavy machine for mixing and resampling, and while it is easy to use like on a big machine, there are the same problems as in the big machine which are... mixing and resampling. This means "near real" Paula has to be done on a dedicated DAC engine to avoid these problems

    Paula generates audio like Pokey: by dividing the base frequency by a register value.... and even the clock base is the same or similar (1.79 MHz for Pokey) - derived from the pixel clock. This means... Paula and Pokey can work on the same sampling machine.

    Someone said 8-bit Atari is a smaller, older Amiga... with its Antic - display list and Pokey which can work as a 4 channel, 4 bit sampler...

  • Ahle2Ahle2 Posts: 1,149

    @pik33

    You could change this line in the simpleSound start() method...

      ' Initialize the reSound sound driver/mixer
      reSound.init(clkfreq, mixingFreq, false, tracker.NR_CHANNELS + NR_SFX_CHANNELS, leftPin, rightPin, reSound.C_NOFX, false, @resoundHubData)
    

    to...

      ' Initialize the reSound sound driver/mixer
      reSound.init(clkfreq, mixingFreq, true, tracker.NR_CHANNELS + NR_SFX_CHANNELS, leftPin, rightPin, reSound.C_NOFX, false, @resoundHubData)
    

    That way reSound will be initialized to always use PWM mode and to lock the mixing period to multiples of 256 system clocks for the best sound quality. The reason why it isn't used by default, is because that will decrease the accuracy of the fine tune for the whole Protracker routine. You will hear that the music will change in pitch, especially at higher mixing rates where the mixing period will be up to 255 system cycles off to allow the PWM mode to work optimally.

  • Ahle2Ahle2 Posts: 1,149
    edited 2021-02-09 10:29

    @pik33
    Forget about the post just above and delete everything from the first zip-file and download the latest version from the top!

    It solves the issue that the PWM mode isn't enabled by default. It also does all calculations correctly to playback modules and samples at the correct rate when the HQ mode is enabled.

    Please just forget about the first version! :wink: Okay?!

  • pik33pik33 Posts: 942

    Ok :) We all know there is always another bug :)

    The audio capabilities of P2 are awesome. It seems I have to buy another AV accessory card.

    I downloaded and compiled the new beta, but to compare anything I have to return home, where there are good headphones and an amplifier

  • Ahle2Ahle2 Posts: 1,149
    edited 2021-02-09 11:32

    @cgracey said:
    Ahle, any chance you could make a YouTube video just to demonstrate the sound capability? I mean, something one could just click on to get an idea of what we're talking about here?

    It doesn't have to explain anything, but just give a sound sample.

    I think the sound quality would be very good, way beyond people's normal 8-bit expectations.

    I will definitely do a YouTube video of it in action! :smile:

  • pik33pik33 Posts: 942

    I tried to get audio out of these 4 RCA/Cinch sockets. It works. We can have amplifier + headphone or 5.1 out of this board if it is not used for VGA video.

  • Ahle2Ahle2 Posts: 1,149

    I did use that board when developing reSound to test out the surround sound. It works great! :smile:

    @pik33

    Put this is in and it will quantize the calulated mixing freq correctly in HQ mode...

      HQ_MODE_QUANTIZE_MASK      = ENABLE_HQ & !255
    

    and then...

    mixingFreq := clkfreq / (((reSound.CYCLES_PER_CHANNEL * (NR_SFX_CHANNELS + tracker.NR_CHANNELS)) + reSound.CYCLES_MAINLOOP_OVERHEAD + HQ_MODE_OVERHEAD) & HQ_MODE_QUANTIZE_MASK)
    
  • Ahle2Ahle2 Posts: 1,149

    Running the P2 at 252 MHz and configured for 4 channels you will get a respectable 492.187 kHz mixing rate and with your overclocking experiments at 370 MHz it goes up to 722.656 kHz. Quite good and I don't think you will get better sounding mod playback on your normal desktop player. Even with all kinds of interpolation going on.

  • cgraceycgracey Posts: 13,406
    edited 2021-02-09 14:55

    It's the local 3.3V LDO regulators that enable the good sound quality. Having quiet power is really important for analog pins.

    On the first P2 Eval board, we had a single switching regulator feeding all the Vxxxx pins. You could hear its whine on audio outputs. Von Sarvas found these very quiet 3.3V LDO's (those little black squares around the P2 corners) and they took things to a much higher level.

  • pik33pik33 Posts: 942

    @Ahle2 said:
    Running the P2 at 252 MHz and configured for 4 channels you will get a respectable 492.187 kHz mixing rate and with your overclocking experiments at 370 MHz it goes up to 722.656 kHz. Quite good and I don't think you will get better sounding mod playback on your normal desktop player. Even with all kinds of interpolation going on.

    I use a RPi for modules... the Rpi is programmed without any OS :) but then there are no DACs: only PWM audio. I did what I could, using a noise shaper and 960 kHz sample rate, the maximum I got from RPi's PWM at 8-bit depth.
    This means the Propeller 2 should give much better sound quality having real DACs onboard at similar sample rate and as I can now hear on an entry level speakers, it IS better.

  • pik33pik33 Posts: 942

    I cannot get stereo separation to work: the module sounds the same, no matter what I put there, including 0

  • BaggersBaggers Posts: 2,991
    edited 2021-02-09 23:11

    This is awesome Ahle2 :D

Sign In or Register to comment.