C/C++ Sound OS API [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a question about sound.
I have used sound libraries like OpenAL in my projects before.
What I need, is insight as to what underling OS APIs these libraries use.
Even if each library provides an easy way to manipulate the input file
according to format, the very basic "raw byte-to-byte, send to the driver"
function has to exist.
I mean, surely there has to be a default api (one for windows, another for Linux)
that these libraries use. I don't suppose they use directly each sound
card's drivers, so the OS has to somehow do the magic. Am I correct?
Now, I know DirectX supports sound (Although I have never used it), but
DirectX isn't installed by default on windows, so I suppose it doesn't count,
and I have no idea what happens on Linux, and I would like to know about both.
I know it's probably impractical not to use a dedicated library, and I don't
really intend not to, but I'm curious about this subject.
So please indulge me.
So, for basic graphics it's OpenGL and DirectX... But what about sound?
Thank you in advance.

Each major platform has a number of API's that allow you to work with sound, On Windows and Mac there are Native Sound Api's that are used by default by the OS as well as well as others that are either Non-Standard or deprecated.
Have A look at he diagram HERE, it has a useful breakdown of many of the major sound API's across major platforms.
In addition to each platform having native sound API's there are also many cross platform API's that encapsulate the way that each native API works in order to allow you to write portable audio software.
For example there is: PortAudio which is a well know C language API
there is also RtAudio which is a C++ API for sound, but it is somewhat of an older C++ style in my opinion(Does not take advantage of post C++11 features).
I am currently working on my own more modern C++11 audio API which can be found HERE. At the moment my API is a thin wrapper around PortAudio that allows you to work with audio in a more modern C++ way.
Keep in mind, the library that you choose will also depend on what kind of audio work you intend to do. All of the libraries I have listed above deal with real-time audio processing and do not deal with audio files. If working with audio files is what you are trying to do you could use libsndfile which is a popular open source sound file manipulation API.
From the context of your question it sound like you have been dealing with sound primarily in a Game Dev related context. It's worth mentioning that working with sound at the level that most of the libraries I have suggested thus far will be at a much lower level than simply calling one function to play a sound file.
Trying to answer the question of how OpenAl interacts with the OS is an answer best left to reading the OpenAL documentation.
I would also suggest looking into basic digital audio theory as well as digital signal processing. There are many resources available for free online on either subject.
EDIT:
In regards to how audio API's work... The average audio api works off of several layers of abstraction between the programmer and the sound card. Typically the programmer is given a buffer of audio data stored as an array of values. The programmer will have requested a specific set of parameters that the system will use for playback (sampling rate, buffer size, number of channels). The programmer will do their work with the audio data and hand the output buffer over to the api which in turn will eventually hand the buffers data over to a device driver written specifically for the installed sound card. The driver for that sound card will have been implemented based off of an interface specified by the platform that the driver is targeting. That is why when you install a new sound card on a machine you may be required to install drivers for it, by installing the drivers you are giving the OS level api a means of communicating with the device.
(There is a lot more that goes on than is possible to easily explain, and i'm sure i have missed a few steps in the process. But I hope that should be a good enough explanation to get started)

Related

Interfacing with a custom PCI from windows [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm trying to write a program that can read from a bunch of sensors, make decisions on a windows computer, then write to some actuators (e.g. valves). This is all already done on an existing system and program but we want to do something different and so are starting with a new program on the windows computer from scratch.
The reading and writing of sensors etc is done through a custom-made PCI from the manufacturer and basically I'm not sure how to read and write to the registers on the board. I've done projects with Raspberry Pis and other microcontroller+PCBs but this one's a bit new. The original program was written in c++ if that helps.
Any general or specific info on interfacing directly with PCIs from windows systems would be great, and if this isn't enough info let me know and I'll provide more. Thank you!
You need to access the hardware via a driver provided by the manufacturer. Many manufacturers don't document how to access their drivers directly by the end user code. Instead, they provide an API library (in an SDK) that the users call into to operate the PCI device.
It'd help if you'd post a link to the device you're using - that way we could look at what's bundled with it and help you decide whether it's worth the effort. If the manufacturer is non-cooperative, this may be a relatively hard thing to do. You may have more luck not using the PCI card and e.g. getting some EtherCAT I/O modules, using an open source EtherCAT master library, and connecting your application to the I/O modules via a simple network card. That would be way more supportable, and very resistant against obsolescence, as the EtherCAT I/O is not going away anytime soon, and your code could be fully portable if you used a portable app development framework like Qt or wxWidgets. You could then run it on PCs, macs, Raspberry PI - and it'd do the same thing and look mostly the same. It's not a pipe dream - I maintain some code that does pretty much just that and it's a viable approach. As long as your requirements are "soft" realtime, it's bound to work. If you need hard-realtime, you'll need to limit yourself to Linux with realtime extensions, and access the network interface (using the ECAT master library) from a realtime thread. It's still more developer-friendly than, say, using a PLC with EtherCAT Master support - those usually use CODESYS under the covers, and that's basically just spreading misery :(
In my experience, supporting some custom motion-control PCI cards and such ends up being semi-rewarding, since you get to do some reverse engineering all too often, and that's a good challenge, but it's a dead-end ultimately. If the card has no API/SDK available, then it's best assumed that the manufacturer is dumping it, and all your work will be for nothing when it goes out of support or becomes unobtainium. It's best to rely on viable industry standards, and on interfaces that have multiple vendors on equal footing. EtherCAT I/O is dime a dozen, almost. You can use e.g. the (freely downloadable) TwinCAT to explore the I/O module's functionality and prototype stuff, and then implement it in your own code using the ECAT master library, perhaps with help of WireShark when needed.

Graphics for C++ cross-platform music making software [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
EDITED, see below
I need an library to display graphics of music notation on a music making software. It will be Sibelius-like: Basically the user creates the sheet music and the app sends midi output.
I'm not looking for an library to display music notation, as for various reasons i must implement it myself. Also, the library i'm looking for must be cross-platform (should run at least on Windows and Linux).
The graphics will be vector-based or drawn with functions like "line(point1, point2);"
(Before continuing let me state that i'm new to C++ and most of my experience is with interpreted languages - PureData and Processing, mostly)
Now to the question:
I've found Cinder. You think Cinder is sutiable for the task? If no, you might skip 2 and 3.
I don't have experience with OpenGL. I couldn't actually figure out if Cinder uses OpenGL exclusively or if it uses it only on demand. My doubt here is, when using OpenGL libraries my application will only be able to run on computers with dedicated graphics cards?
Cinder doesn't have the hability to create GUIs like Qt or GTK+. Is it possible to integrade Cinder in another window, created by Qt/GTK+/SDL/etc ? I want something like Sibelius, where you have normal GUI (buttons, menus, etc) integrated in the notation graphics window.
If Cinder is not a good option, what might you recommend for that specific application needs? OpenCV? SDL? SFML?
EDIT:
I see this was marked as off-topic. Sorry, i'm new here. I've seen some posts asking for this kind of recomendation so i thought it was okay. Well, i will try to be objective and straight-forward
Question:
I need to draw music notation graphics, but i don't want to deal with low-level stuff (like Cinder, SDL, SFML, Ati-Grain). So, what are the other technologies available? That is, what are the other options?
What you are describing is a LOT of work and something impossible for someone with limited programming experience such as yourself. C++ is neither an easy language nor a language that forgives.. it's not just about learning the syntax (as is the case with processing or similar languages with automatic garbage collection) - it's also about managing memory and other resources, invariants, etc - leaving aside the complexities of linkage, compilation, etc: in turn this prerequisites that you have a thorough understanding of how computers work at a lower level.
Anyway, I'll try to answer your questions:
You could do this with cinder - this would probably mean having to re-invent the wheel - but you can do. I wouldn't say, however, that cinder is really suitable for such a task. This is meant for accelerated 2D/3D computer graphics. What you want is some more generic GUI library (maybe QT?). Regarding midi-support, you'll probably have to rely on some other library for this anyway.
Most computers these days do support openGL. Again you don't really need openGL for what you want to do.
Yes you can do this in theory - but it won't be easy and it's not really necessary for what you want to do.
OpenCV definitely NOT. This is for image processing - i.e. raster graphics - you need vector graphics. Let aside that OpenCV's Highgui is really slow on certain things so you'd have to use some other library for rendering anyway. I have no experience with the other libraries. My suggestion would be to either use some interpreted language such as python, luaAV or maybe SuperCollider (which does have built-in midi support by the way) which is far easier than C++, or to search for some open-source music-notation alternative (if there any) -> you can always build something of your own upon an existent code-base - reinventing the wheel is not necessary...

Looking for a simple audio playback library for C++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
I'm working on a simple audio player, which is going quite well, using Qt and everything. But I'm in need of a solid but simple Audio library. I don't need anything fancy such as 3D sound and what do I know what else these things got nowadays. I'm just looking for something simple and efficient.
A list of features I'm looking for:
Audio playback from files
.ogg, .mp3 and .flac should be supported at the very least.
Should be able to pause playback.
I am planning to use it in C++
See. Nothing huge or anything, but at the same time I am having a hard time finding something suitable. The platform I'm targeting is Windows 7.
Anyone out there that knows something that I might be looking for?
The best possible choice in my opinion is the BASS audio library, which is not open source, but free for non-commercial use.
It is used by the AIMP2 audio player, which you can use to check the sound quality, natively supports a lot of formats including MP3 and OGG, and uses plugins to support even more, including FLAC.
How about libavcodec ? Its LGPL. I'm not sure about ogg >_<
I have used BASS before from a Windows C++ program, and it is very simple to use - link the DLL and call a couple of methods to specify the file to play back.
It certainly handles MP3 playback from a file as that's what I was doing.
I wish I could verify OGG and Flac, but alas I'm behind a firewall that has the site blocked.
Miles is a good and commonly used audio library.
Came across a good answer to my own question via an answer of another. FFmpeg is almost exactly what I'm looking for. Thanks all.
I suggest Openframeworks or use of the rtAudio library - that openframeworks is using - directly. Oh, and on the side: rtAudio has pretty much no license restrictions. ;-)
Correct me, if I'm wrong.
DirectShow is actually pretty easy to use, flexible, and comes with Windows. There are a million examples, and it can be as easy or as sophisticated as you need it to be. When I throw together something that needs to play MP3s, I use it. Note that you are targeting Windows 7, so you could consider using Media Foundation instead.
I use a C++ encapsulation of FFMPEG : FOBS
Easy to use and C++ :)
EDIT:
Fobs is no longer maintained. However you can use it as a starting point to make your own C++ encapsulation of FFMPEG.

Open source libraries for sound effects in games [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 8 years ago.
Improve this question
does aybody know about an open source sound library in C++ or some other popular language that can be used in open source games for recreating sounds and especifically in car racing games to recreate engine sound?
Thanks
A long time ago, there was OpenAL and was moderately successful on Linux.
It's fairly easy, modelled around the OpenGL library principles.
While you got a few answers regarding how to output sound I believe that wasn't your question.
If I got your question correctly I think "Sound Synthesis" is what you are looking for.
All you need is a bunch of generators (e.g. Sine, Sawtooth, Noise)
and filters (low,high,band); And combine these.
Next step would be adding LFOs (Low-Frequency-Oscillators) to modulate parameters of the generators and filters.
I'm aware that this is not the full answer you looked for, but sound synthesis is a science in itself and the above keywords should get you started on the right track.
As for realtime synthesis of SFX for games. Simple answer:
Don't do it!
It's a waste of resources and - unless you have very strict size limits - the size of the samples used for SFX won't kill you.
For our current game the average sample has a(n on disk) size of around 7KB.
(We use 16KHz, mono, ADPCM wavs most of the time.)
I've only trivially poked around with it, but SDL is a very popular, cross-platform, C++ game dev library. It includes support for graphics, sound, input.
I also recall that it's very modular. That is, it's less of a framework and more of a library than some other solutions. Give it a look.
Is there a specific reason you need open source, or is "source available" good enough? A popular one is fmod http://www.fmod.org
It is free for non-commercial products, and also offers sourcecode under a license. It is very popular and isused in many well known games.
The irrKlang sound engine is very popular amoung independent developers. It is free for non-commercial use and still very cheap, if you want to start earning some money with your game.
It supports all major plattforms (Windows, Linux, Mac) and has APIs for C++ and .Net languages.
I know it's an old question, but I think SFML needs to be represented here. SFML is available in C++, C, .Net (C#, VB.Net, C++/CLI, etc), Python, D, and Ruby, and is designed to be small and fast.
Features specific to audio:
Uses hardware acceleration whenever possible
Can load and save standard sound formats : ogg, wav, flac, aiff, au, raw, paf, svx, nist, voc, ircam, w64, mat4, mat5 pvf, htk, sds, avr, sd2, caf, wve, mpc2k, rf64
Can load all audio resources directly from files in memory
3D sound spacialization
Easy interface for capturing audio
Manages memory efficiently, so that you don't have to worry about resources lifetime or storage
Supports streaming for big files ; you can even write your custom streaming class for any source (network, ...)
Supports multi-channels formats (mono, stereo, 4.0, 5.1, 6.1, 7.1)
For the specific question of recreating engine sounds, usually games take a single looping sound and adjust the pitch. An open source game that does this very well is VDrift.
SDL is Good Option for Graphics/Sound
QT frame work has also good support for sound and graphics
PortAudio is also good option

cross platform game development what to look for?

I am going to start a game in about 3 weeks and I would really like the game to run at least on another platform (linux, MacOS) but my team thinks that's a lot of work. I am up for it but wanted to know what are the things I should watch out for that won't port to linux (apart from Windows specific APIs like DirectXsound)?
I've been reading online and Windows "_s" functions like sprintf_s appear to exist only on Windows; is this correct or are they implemented on linux also?
No, the _s functions are NOT implemented in the standard gcc library.
(At least, grepping the include files for 'sprintf_s' turns up nothing at all.)
It might be worth looking at cross platform libraries like boost and apr to do some of the heavy lifting work.
A sample of specific things to look for:
Input/Output (DirectX / SDL / OpenGL)
Win32/windows.h functionality (CreateThread, etc)
Using windows controls on the UI
Synchronization primitives (critical sections, events)
Filepaths (directory separators, root names)
Wide char implementations (16 bit on windows, 32bit on linux)
No MFC support on linux (CString, etc)
If I were you I would use some of the available Frameworks out there, that handle Platform independence.
I wrote a 3D-Game as a hobby project with a friend of mine, the server being in Java and the clients running on Windows and Linux. We ended up using Ogre as 3D-Engine and OpenAL as Sound-Engine, both platform independent and available under LGPL.
The only things I really had to write separately were the whole Socket-handling, reading the config from file system and the initialization of the System. Compared to the rest of the Program, that was almost nothing.
The most time consuming will be to set up the entire project to compile under Windows and Linux (or Mac), especially if you're concentrating on one and only occasionally check the other for problems. If you have one in your team who checks regularly for these problems while they're being produced you won't have that much overhead from that as well.
All in all compared to the programming of the game itself, adapting it to different platforms is almost no effort, if all frameworks used are well written, platform independent systems.
Try to encapsulate any non-standard extentions like DirectX, OpenGL, SDL, etc. Then you only have to rewrite those parts based on platform.
I also would make it playable on one OS before even thinking of porting.
For the 'safe' functions: they are non-standard, and almost safe :)
Endianess is something look out for.
Endianess is the order of the bits in a byte. Some platforms are big endian while some are little endian.
This can affect how cross-platform your program is. But the biggest impact this would have would be in network communications. You have to convert from one endian to another before sending or receiving a network message.
If you focus on gameplay, design a game, and them implement that porting should not be especially onerous. If you implement it simultaneous on several platforms it should be straight forward.
But if you focus on effects, design something that you feel is going to "blow the others out of the water," and try to paste a game idea onto them, you are doomed.
So really it is up to you.
Don't know much about windows-apis, but I set up a daily (or on-commit) fully automatic build-system on all platforms you want to support. If you develop something on your windows-box that doesn't work on the others, your build-system should notify you of "failed build on platform x, see logfile/attachment/whatnot for details". It'll catch a lot of cross-paltform issues. Unittests will help as well.
Whether or not to target multiple platforms from the start is a good idea is another question.
Personally I'd start developing on another platform and then see about porting it to windows at a later time ;-)
Just remember that you are creating a model of game that does not depend on the details of any operating system. Your game depends on state management and algorithms which don't depend on the OS. The key is to write your game logic without dependencies to specific libraries which means a lot of encapsulation.
You shouldn't call sprintf_s directly you should write an routine, class, or MACRO that can be changed based on the platform, Don't use DWORD when you can use a class or typedef that can be tailored to different platforms.
For instance if you where making a football game, then algorithms for throwing the ball, running, tackling, positions of the players could be done completely in standard C++ without platform dependencies. If your encapsulation was good you could dump the state of you game to a file and display it separately with a rendering program.
If you truly want to do cross platform development easily I would suggest using one of the already built cross-platform engines like Unity or one of the Garage Games stuff like Torque Game Builder (2D).
I have virtually zero experience in either so can't tell you which is better but the Torque Game builder demo couldn't get through the first tutorial without having problems and they don't answer tech support questions in their forums like they claim to do so I can say avoid them if you are a novice in game design like myself. The big thing about Garage Games was supposed to be their great support, I saw zero support and in fact only saw a bunch of, "Hey, anybody here?" posts with no answers so I guess they are pretty much giving up on supporting their products.
http://unity3d.com/
http://www.garagegames.com/
I'm surprised nobody mentions libSDL and OpenGL because most cross platform games were written using those libraries.
If your game is 2D, you can use libSDL. A good example of game written using it is The Battle of Wesnoth. SDL uses DirectX on Windows, it's just a thin wrapper on it.
If your game is 3D, use OpenGL. For example, Quake 3 uses that library. You can find tons of examples and documentation on it. Of course, there are many libraries that wrap OpenGL, so you don't have to do low-level stuff. Look into OGRE, Crystal Space, etc.
As for the basic C/C++ libraries and functions compatibility, it's best you install some Linux and simply run man page for the function to see if it exists. Of you can look it up on the Internet.