simplest method for 32x sound?

Ask anything your want about the 32X Mushroom programming.

Moderator: BigEvilCorporation

Post Reply
pw_32x
Interested
Posts: 19
Joined: Thu Dec 16, 2021 12:26 am

simplest method for 32x sound?

Post by pw_32x » Mon Oct 03, 2022 2:21 am

I've been experimenting with sound on the 32X and nothing I try works. Sometimes I get clicking.
What's the most basic way to get anything out of the 32x sound?

I've got the same commonly used init code.

Code: Select all


#define SAMPLE_RATE      22050
#define SAMPLE_MIN       2
#define SAMPLE_CENTER    517
#define SAMPLE_MAX       1032
	

   // init the sound hardware
    MARS_PWM_MONO = 1;
    MARS_PWM_MONO = 1;
    MARS_PWM_MONO = 1;
    if (MARS_VDP_DISPMODE & MARS_NTSC_FORMAT)
        MARS_PWM_CYCLE = (((23011361 << 1) / (SAMPLE_RATE) + 1) >> 1) + 1; // for NTSC clock
    else
        MARS_PWM_CYCLE = (((22801467 << 1) / (SAMPLE_RATE) + 1) >> 1) + 1; // for PAL clock
    MARS_PWM_CTRL = 0x0185; // TM = 1, RTP, RMD = right, LMD = left

	unsigned short sample = SAMPLE_MIN;
	unsigned short ix;

	// ramp up to SAMPLE_CENTER to avoid click in audio (real 32X)
	while (sample < SAMPLE_CENTER)
	{
		for (ix = 0; ix < (SAMPLE_RATE * 2) / (SAMPLE_CENTER - SAMPLE_MIN); ix++)
		{
			while (MARS_PWM_MONO & 0x8000); // wait while full
			MARS_PWM_MONO = sample;
		}
		sample++;
	}

And after that, I just want to feed the output the simplest way possible. Eventually I'll take a better look at the interrupt driven dma style of playing but right now I just want to play a tone or something.

Code: Select all


void main ()
{
    while (1)
    {
    	// somehow feed a tone to the audio hardware
    	// set something to MARS_PWM_MONO, maybe?
    }
}

Maybe I'm being naive and it's not so simple :)

Chilly Willy
Very interested
Posts: 2984
Joined: Fri Aug 17, 2007 9:33 pm

Re: simplest method for 32x sound?

Post by Chilly Willy » Mon Oct 03, 2022 2:25 pm

The simplest (IMHO) is to make a double-buffer that is DMAed to the PWM registers by one of the SH2 DMA channels, then make routines to fill those buffers. Here's my XM player examples for the 32X - one uses the secondary SH2 in a loop watching the DMA registers to see when to fill the buffers, and the other uses the DMA transfer done interrupt on the secondary SH2 to see when to fill the buffers.

https://www.mediafire.com/file/kbru3l9u ... op.7z/file
https://www.mediafire.com/file/7z2m4uwo ... tr.7z/file

Note that these are kinda old, and the interrupt driven version may have problems on certain 32X boards with the buggy SH2 revision chips. The code in Doom 32X Resurrection shows the latest unified interrupt code with the proper DMA interrupt handling to work on all models of the 32X. So I suggest you also look at the D32XR code after you understand the examples posted above.

Post Reply