• Updated 2023-07-12: Hello, Guest! Welcome back, and be sure to check out this follow-up post about our outage a week or so ago.

Outputting samples to the Mac SE sound chip without using Sound Manager

8bitbubsy

Well-known member
How can I output samples to the sound chip on a Macintosh SE without using Sound Manager? I assume the sound buffer in RAM is not mapped directly to the hardware, but is instead set up and handled by Sound Manager itself?

The reason I want to skip Sound Manager entirely is to create as little overhead as possible, as my program will take up the vast majority of available CPU time by mixing/resampling audio. I know taking over the audio is not very multitasking friendly, but I need all the CPU time I can get on an 8MHz 68000.
 

Phipli

Well-known member
sound buffer in RAM is not mapped directly to the hardware
It is :) there are actually two, that are switchable. But take care, floppy drive speed info is stored in the primary buffer, so you need to make sure you switch back to it.

The buffer is in main memory.
 

8bitbubsy

Well-known member
Oh neat, but how would I tell Sound Manager to stop writing to it, so that I have full control? Also, do you know the linear addresses for these buffers?
 

Phipli

Well-known member
Just checked, the SE dropped the second buffer, it was only available up to the Plus.
Oh neat, but how would I tell Sound Manager to stop writing to it, so that I have full control? Also, do you know the linear addresses for these buffers?
The address changes depending on how much RAM is installed, but it is stored as a global variable. See chapter 13 in that book I linked.

Screenshot_20230707_172411_Drive.jpg
 

Phipli

Well-known member
Note it is every second byte - the low byte of each word is the flopy drive speed because... thats how things are when you use the CPU to control the floppy drive and implement variable speed floppy drives to increase capacity.
 

8bitbubsy

Well-known member
I see. Guess I'll have to do a lot of Gestalt checking and have custom code for Macs before SE, and after SE. :ROFLMAO: But one step at the time. I'll target my first version for Macintosh SE, and if it will work as expected, I'll add some more flexibility (like using 1 Sound Manager voice on 68020 and later CPUs).

Is there any way I can check the live audio buffer position, so that I can know where to put my chunk of sample data for later play? I checked the book now, I can't find any info on it. It may be in there somewhere, though...
 

8bitbubsy

Well-known member
Oh and another thing I've been wondering about. Timing on 68k Macs seems to be done either through Time Manager or the vblank callback vector, but the vblank rate is not guaranteed to be 60Hz, is it? Some Mac monitors ran at 66-67Hz, for example. How would I detect the nominal vblank rate?
 

8bitbubsy

Well-known member
Sorry for triple-posting, but I just found out that the sound buffer is 370 bytes long (ignoring the floppy bytes), and that this whole buffer is written to the sound chip per frame. Now my problem is, I would have to play a racing game where I make sure to have my bytes written in time before they are written to the sound chip. This sounds complicated, so I think I'll have to drop support for 68000 Macs entirely, and use Sound Manager and 68020+ Macs instead. :(
 

Phipli

Well-known member
I see. Guess I'll have to do a lot of Gestalt checking and have custom code for Macs before SE, and after SE.
I.... think the newer ones are backwards compatible - they gave them a compatibility mode... I believe.

@Crutch might know?

vblank callback vector, but the vblank rate is not guaranteed to be 60Hz, is it?
Compact macs have a fixed rate, it isn't exactly 60hz though. The timings are in the book. Check the audio and the video sections.
Is there any way I can check the live audio buffer position, so that I can know where to put my chunk of sample data for later play? I checked the book now, I can't find any info on it. It may be in there somewhere, though...
Not sure sorry.

Crutch?

Now my problem is, I would have to play a racing game where I make sure to have my bytes written in time before they are written to the sound chip. This sounds complicated, so I think I'll have to drop support for 68000 Macs entirely, and use Sound Manager and 68020+ Macs instead.
You could prepare the 370 samples elsewhere and copy them over during vblank? Or have a larger buffer and keep copying over 370 samples each vblank. it will increase latancy, but that isn't the end of the world.

If you're really short of time you could just... erm... sample chase, or whatever you'd call it. Its fixed speed so if you got the rate right and reset your timing by every vblank its going to remain reasonably constant. I suggest hogging the processor and not letting other processes use too much CPU time though.
 

Crutch

Well-known member
How can I output samples to the sound chip on a Macintosh SE without using Sound Manager? I assume the sound buffer in RAM is not mapped directly to the hardware, but is instead set up and handled by Sound Manager itself?

The reason I want to skip Sound Manager entirely is to create as little overhead as possible, as my program will take up the vast majority of available CPU time by mixing/resampling audio. I know taking over the audio is not very multitasking friendly, but I need all the CPU time I can get on an 8MHz 68000.
@8bitbubsy , what you are describing here is “I want to use the Sound Driver not the Sound Manager.” The original Sound Driver’s ”Freeform synthesizer” is for exactly this - giving the CPU a pointer to a block of memory you want copied piecewise into the 370-byte buffer. It uses about 20% of the 68000, is extremely simple and I doubt you would improve performance noticeably by rewriting it yourself (which is what copying data into the buffer and keeping track of the “playback head” yourself would entail).

Everything you should need is in the Sound Driver chapter of Inside Macintosh Volume II. Let me know if you have specific questions.

(Note that using the Sound Driver directly was deprecated in the Mac II era due to bugs and compatibility issues with the Apple Sound Chip and fancier later tech though in practice continued working for a while.)
 

Crutch

Well-known member
Addendum: the Sound Driver on the Plus and SE had issues with a “click” sometimes when sounds started/stopped. The solution to this was to continuously play silence when you weren’t actively playing sound (basically zero out the Freeform buffer and loop forever). This is highly discouraged under MultiFinder/System 7 because it eats up CPU, but is OK in a single-tasking environment. I can’t find the original tech note for this, but here’s the “don’t do that any more” follow-up TN that touches on the issue: https://spinsidemacintosh.neocities.org/tn405#tn019
 

8bitbubsy

Well-known member
I'm already giving up on 68000 support. Mixing four voices at 22254.54Hz with my asm code probably takes up more than 80% of the CPU time on a 68000 8MHz, I don't really have room for another stage (buffer copying) now that I think about it.

So I'm going to target 68020+ and use one Sound Manager voice, and I'll use a vblank interrupt/callback for player timing, I think. However, like I asked earlier, how do I detect the vblank rate? Some Macs have 66-67Hz video modes (12" Apple RGB monitor etc.), and that would mess with my timings if I expect a nominal 60.15Hz
 

Crutch

Well-known member
Sorry, you want to use VBL for timing of sound playing, or for flicker-free animation?

You generally should not need to know the VBL refresh rate. If you use the Vertical Retrace manager to install a VBL task with _VInstall, it will always run 60 times/second on Macs with the Slot Manager, so the actual video refresh rate doesn’t matter.

If you really need to sync with the actual refresh rate of a particular non-built-in CRT e.g. for flicker-free animation, you need to use the Slot Manager routine _SlotVInstall to do that. See the Slot Manager chapter of Inside Macintosh Volume V.

If you want a callback rate other than 60 Hz and don’t care about syncing with any specific monitor, you should instead use the Time Manager and just specify whatever frequency you want.
1688832067375.png
 

Phipli

Well-known member
Sorry, you want to use VBL for timing of sound playing, or for flicker-free animation?

You generally should not need to know the VBL refresh rate. If you use the Vertical Retrace manager to install a VBL task with _VInstall, it will always run 60 times/second on Macs with the Slot Manager, so the actual video refresh rate doesn’t matter.

If you really need to sync with the actual refresh rate of a particular non-built-in CRT e.g. for flicker-free animation, you need to use the Slot Manager routine _SlotVInstall to do that. See the Slot Manager chapter of Inside Macintosh Volume V.

If you want a callback rate other than 60 Hz and don’t care about syncing with any specific monitor, you should instead use the Time Manager and just specify whatever frequency you want.
View attachment 59123
They're asking because they want to know when is best to update the audio buffer. On the 68000 macs, you're told to do it during vertical blanking, but on a Mac II series mac... when is best to do it?
 

8bitbubsy

Well-known member
I don't really need to sync with vblank for timing, but it just sounded convenient and simple. I guess I can just setup a 50Hz (or any arbitrary value) timer with Time Manager instead. Good to know that vblank callback is 60Hz regardless of the actual vblank rate, though. Pretty strange, but I guess it does some waiting or something to make sure it's 60Hz even on 66-67Hz rates.

Also @Phipli: I'm a guy, so now you don't need to use "they" again. :)
 
Last edited:

Phipli

Well-known member
Also @Phipli: I'm a guy, so now you don't need to use "they" again.
Sure thing - apologies if I forget at any point, almost everyone on a forum is anonymous really, so I use the correct grammar. This is not a modern gender thing.
 

Crutch

Well-known member
Good to know that vblank callback is 60Hz regardless of the actual vblank rate, though. Pretty strange, but I guess it does some waiting or something to make sure it's 60Hz even on 66-67Hz rates.

It doesn’t do any waiting, it’s just completely independent of vertical blacking. On Macs with the Slot Manager, the retrace interval of whatever monitor you happen to be running doesn’t impact when system-required tasks like the Stack Sniffer or updating TickCount happen (and indeed, you don’t particular want it to), so those things just happen 60 times/second no matter what.
 
Top