Simple audio output

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
onlyonemac
Member
Member
Posts: 1146
Joined: Sat Mar 01, 2014 2:59 pm

Simple audio output

Post by onlyonemac »

How do I do simple audio output, beyond operating the PC speaker? Is there an equivalent of VESA but for audio? You see, most people would at this stage start implementing text-mode or simple graphical output for debugging their operating systems, but just to make things really interesting I'm blind so I need a better debugging output form than morse code. I have written a simple speech synthesiser in the past and porting it to run in a debugger environment would be simple enough if I knew how to get the sound samples from the synth to the computer's audio output.
When you start writing an OS you do the minimum possible to get the x86 processor in a usable state, then you try to get as far away from it as possible.

Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: Simple audio output

Post by SpyderTL »

There's nothing like VESA for audio devices, as far as I know.

Depending on your environment, you can probably either use the old Sound Blaster, which is emulated by most legacy sound cards, or HD Audio, which is compatible with most modern systems.

I've gotten 16-bit stereo audio to play on the sound blaster under VirtualBox, and I'm currently working on getting HD audio working in the same environment. I'm probably 70% of the way there.

The osdev wiki pages for both of these are pretty helpful.

I would stay away from AC97, as it isn't really a PCI device reference specification as much as it is a common embedded register set. You still have to write custom drivers for each AC97 device.
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
onlyonemac
Member
Member
Posts: 1146
Joined: Sat Mar 01, 2014 2:59 pm

Re: Simple audio output

Post by onlyonemac »

SpyderTL wrote:I've gotten 16-bit stereo audio to play on the sound blaster under VirtualBox, and I'm currently working on getting HD audio working in the same environment. I'm probably 70% of the way there.
Perhaps you could share your sound blaster code with me?
SpyderTL wrote:The osdev wiki pages for both of these are pretty helpful.
That is somewhat of an overstatement.
When you start writing an OS you do the minimum possible to get the x86 processor in a usable state, then you try to get as far away from it as possible.

Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: Simple audio output

Post by SpyderTL »

All of my code is available at the link in my signature. But it is mostly written in my own XML based language, so it may take some getting used to. It's similar to ASM, so it should be pretty obvious what is going on. If not, let me know.

I'm working on the HD Audio wiki page, so check back in a day or two and it should have some more information.

Soundblaster is definately easier to code against, though. Here is a link to the code you are looking for: https://ozone.codeplex.com/SourceContro ... laster.xml
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
mallard
Member
Member
Posts: 280
Joined: Tue May 13, 2014 3:02 am
Location: Private, UK

Re: Simple audio output

Post by mallard »

Another good source would be any audio code written for DOS. Since DOS is such a primitive OS, applications/games that need to use audio have to implement virtually all the hardware support themselves (SoundBlaster cards often came with a "driver" for DOS, but this did little except detect/configure the card's I/O ranges and set the "BLASTER" environment variable accordingly).

There are DOS sound libraries that support SoundBlaster, common AC97 chips and Intel HDA. The "WSS" and "JUDAS" libraries are examples (working download links can be hard to find though, I found them here). Both are GPL licensed, although JUDAS doesn't specify which version. WSS seems to be more straightforward code-wise.
Image
onlyonemac
Member
Member
Posts: 1146
Joined: Sat Mar 01, 2014 2:59 pm

Re: Simple audio output

Post by onlyonemac »

SpyderTL wrote:All of my code is available at the link in my signature. But it is mostly written in my own XML based language, so it may take some getting used to. It's similar to ASM, so it should be pretty obvious what is going on. If not, let me know.

I'm working on the HD Audio wiki page, so check back in a day or two and it should have some more information.

Soundblaster is definately easier to code against, though. Here is a link to the code you are looking for: https://ozone.codeplex.com/SourceContro ... laster.xml
Your XML-style assembler looks interesting, by the way, but without comments it's difficult to understand what the code is doing.
mallard wrote:Another good source would be any audio code written for DOS. Since DOS is such a primitive OS, applications/games that need to use audio have to implement virtually all the hardware support themselves (SoundBlaster cards often came with a "driver" for DOS, but this did little except detect/configure the card's I/O ranges and set the "BLASTER" environment variable accordingly).

There are DOS sound libraries that support SoundBlaster, common AC97 chips and Intel HDA. The "WSS" and "JUDAS" libraries are examples (working download links can be hard to find though, I found them here). Both are GPL licensed, although JUDAS doesn't specify which version. WSS seems to be more straightforward code-wise.
I looked at the source code for WSS but there's so many different functions that it would take ridiculously long to figure out what they're all doing.

Essentially what I'm looking for is a step-by-step guide for how to, in the simplest way possible (I don't need surround sound, multiple line-in, etc.; just basic audio output)
  • detect and configure an audio card
  • tell the audio card to play
  • send data to the audio card for playing
. It doesn't matter if this comes in the form of code, as long as it's clear what each function in the code does and what the various lines are doing (especially assembler - it's not easy to learn from assembler code because you don't know what lines are working with the hardware and what lines are for whatever internal interfaces the developer has chosen to use).
When you start writing an OS you do the minimum possible to get the x86 processor in a usable state, then you try to get as far away from it as possible.

Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: Simple audio output

Post by SpyderTL »

There's always the Linux Soundblaster drivers. http://lxr.free-electrons.com/source/sound/isa/sb/

Most Soundblaster cards can't be autodetected, because they aren't PCI or plug n play devices. You'll just need to hard code the IO port number, for now. It'll either be 0x220 or 0x240.

You can try to get the version number using those ports, and if you get a valid response, you can consider the card "detected", but I wouldn't rely on this in the long term.

Once you have found the port number, you need to reset the device, and then tell it what format your audio data will be in. (16-bit stereo PCM, for example)

Then you'll need to find the correct DMA channel to use (you should just hard code this as well). It will probably be channel 12 if you want to do a 16-bit transfer. Then you just setup the DMA channel to point to an address containing your audio data.

Then you send a command to the Soundblaster that contains how many samples to play, and it will send you an interrupt when it's done. Then you just refill the buffer, and it will continue playing until you send it a command to stop.

The specific details are on the wiki page. Let us know if you have any specific questions.
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
onlyonemac
Member
Member
Posts: 1146
Joined: Sat Mar 01, 2014 2:59 pm

Re: Simple audio output

Post by onlyonemac »

SpyderTL wrote:Most Soundblaster cards can't be autodetected, because they aren't PCI or plug n play devices.
Umm... I don't have a SoundBlaster card. Does this information apply to the SoundBlaster compatibility mode on more modern cards? So like, would this apply if I try to drive onboard audio on a 2003 PC like a SoundBlaster card? According to Linux, that onboard audio is a PCI device.
When you start writing an OS you do the minimum possible to get the x86 processor in a usable state, then you try to get as far away from it as possible.

Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: Simple audio output

Post by SpyderTL »

Probably not. I think SoundBlaster compatibility disappeared when AC97 became popular.

But there's an easy way to check. If you are running Windows, open up the device manager and see if your sound card is using I/O port 220 or 240. If so, then it's going to be SoundBlaster compatible.

If not, I would recommend running your OS under VirtualBox and using its SoundBlaster audio mode until you get your HDAudio code working.
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: Simple audio output

Post by SpyderTL »

onlyonemac wrote:Your XML-style assembler looks interesting, by the way, but without comments it's difficult to understand what the code is doing
I'll admit that most of that file is missing comments, but the important code that you are looking for (the "play" method) is commented pretty well (compared to the rest of the file, at least).

Just pulling out the XML comments, you can see the step by step tasks needed to play audio on a SoundBlaster:
  • Fill Buffer
  • Disable DMA
  • Reset DMA
  • Set Source Address
  • Reset DMA
  • Set Count
  • Set Page (0x0)
  • Set Mode (Single Transfer, Auto Initialize)
  • Enable DMA
  • Wait For Busy
  • Set Sample Rate (44100)
  • Send Output Command (Output, Auto Initialize, FIFO Enabled)
  • Send Mode (Stereo, Signed)
  • Send Sample Count (0x800)
  • Wait For Interrupt
  • Fill Buffer
  • Turn Off Audio
The step-by-step comments are there for anyone to see. What are you? Blind??

Hehe. :)

If you decide to go the HD audio route, things are a lot more difficult, but hopefully I'll have my code working in a few days, and I'll be able to help out a little. I added some more information to the HDAudio wiki page last night, but it still needs a little more information to get everything working properly.
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
onlyonemac
Member
Member
Posts: 1146
Joined: Sat Mar 01, 2014 2:59 pm

Re: Simple audio output

Post by onlyonemac »

HD audio is not going to help me. FWIW, I'm testing the operating system on a PC from 2003 which uses Realtek AC97 audio. I'd rather not use a virtual machine because I prefer to work with real hardware.
When you start writing an OS you do the minimum possible to get the x86 processor in a usable state, then you try to get as far away from it as possible.

Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing
iamnoob
Posts: 13
Joined: Mon Oct 19, 2015 12:36 pm

Re: Simple audio output

Post by iamnoob »

onlyonemac wrote:HD audio is not going to help me. FWIW, I'm testing the operating system on a PC from 2003 which uses Realtek AC97 audio. I'd rather not use a virtual machine because I prefer to work with real hardware.
With all due respect this thread boils down to "give me da codez!" with you dismissing every reply given to you. My suggestion would be to be up front, specific about your problem, and show what you have already tried.

Helpful links:

http://mattgemmell.com/what-have-you-tried/

http://sscce.org/

http://wiki.osdev.org/How_To_Ask_Questions
Nable
Member
Member
Posts: 453
Joined: Tue Nov 08, 2011 11:35 am

Re: Simple audio output

Post by Nable »

If you have an ISA slot, it's quite easy to find SB16-compatible card.

Good old buzzer (aka PC Speaker) may also be a good way to go. It's rather noisy and it's takes rather much CPU time to drive it for complex sounds but it's still possible to use it for audio output (I hope that it's present in your case).
Another simple and nice way is... COVOX! I don't think that 2003's hardware already lacks an LPT port.
User avatar
DavidCooper
Member
Member
Posts: 1150
Joined: Wed Oct 27, 2010 4:53 pm
Location: Scotland

Re: Simple audio output

Post by DavidCooper »

I think a blind person writing an operating system and wanting to work on real hardware deserves a bit of extra respect and is entitled to ask for help getting over this major hurdle. If the only machine available to him to work on only has AC97 on it, then that is what he will have to program for, unless he can switch to a newer machine with HDA. HDA's been around for a long time now, so I'm sure there will be someone out there selling a suitable machine at low cost, although the biggest problem there will be knowing whether it has HDA before buying it. I'd definitely rather put the time into HDA though as it's more futureproof - it is the most sane option.
Help the people of Laos by liking - https://www.facebook.com/TheSBInitiative/?ref=py_c

MSB-OS: http://www.magicschoolbook.com/computing/os-project - direct machine code programming
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: Simple audio output

Post by SpyderTL »

If AC97 is your only choice, then you'll have to find the specs for your specific device, and write a driver for it. I don't know of any "easy" way to get around it.

Unlike HDAudio or Soundblaster, AC97 doesn't have a standardized interface to the hardware. It's like saying my video card supports HDMI, but that doesn't help me write a video card driver for it. It's probably the most difficult way to go, but let us know how it goes. Maybe you can even add some information to the AC97 wiki page.

Let us know if there is anything we can do to help.
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
Post Reply