I’m pretty new to C++, but have some experience with Python. I wanted to make a program that played certain frequencies based on different conditions, but I soon realized I couldn’t even figure out how to play an audio file. From what I found looking for a solution, it seems almost impossible on Mac, and everything I tried copying into Xcode ended up with a bunch of errors. Where should I start with this?
Question posted in Xcode
Whether you're new to Xcode or an experienced developer, our archive has everything you need to know about this integrated development environment (IDE). From basic functionalities to advanced features, our archive covers a wide range of Xcode-related questions and answers. Browse our archive now and find solutions to your Xcode questions, and take your app development skills to the next level
Whether you're new to Xcode or an experienced developer, our archive has everything you need to know about this integrated development environment (IDE). From basic functionalities to advanced features, our archive covers a wide range of Xcode-related questions and answers. Browse our archive now and find solutions to your Xcode questions, and take your app development skills to the next level
2
Answers
I once used SFML for this. Although I did it on Linux, I see that its available for mac as well. You can go through the tutorials here.
The problem here is that C++ is just a programming language. The same can be said for python, though Python lives in a different ecosystem of modules and package management which get conflated (rightly or wrongly) as part of the language
C++ doesn’t have the same history and the same ecosystem and this is part of the battle you will have when learning it. You don’t have
pip
, you have a nebulous series of frameworks, headers and libraries (some standard, some which need installation) all of which need linked, path-ed or compiled. It is an ecosystem that is unfriendly if you try and approach it like a novice Python programmer. If you approach it agnostically, it is simultaneously very powerful and exceptionally tedious, a combination that tends to polarise developers.This means that simple answers like Use SFML!, SDL, NSOUND, OpenAL, CoreAudio, AVFoundation, JUCE, &c… are all technically "correct" but massively gloss over large parts of setup, nomenclature and workflow that are just a
pip install
away with python.Pontificating aside, if you want to simply
Then you are probably best just
.wav
.wav
withafplay
Is that the most open, versatile, DSP orientated, play-from-RAM solution? No, of course not, but it is a solution to the problem you pose here. The alternative and correct answer is an exhaustive list of every major media library, cross-platform and macOS specific, their setup, quirks and minimum working example, which would result in an answer so obtusely long I hope you can sympathise with why it is not best addressed on Stack Overflow.
A Simple CLI App
You can find all the constituent parts of this on SO, but I have tallied-off so many how do I play a sound in C++ questions it has made me realise they are not going away.
The setup for Xcode is to create a Command Line Tool project (Console App for Visual Studio).
Here is a header that will wrap up everything into a
playSound
functionaudio.h
main.cpp
Your
main
function could then be something like: