Thread: How do you handle sound and music files in C?

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    55

    How do you handle sound and music files in C?

    I do not know any of the functions to do this, and when I looked on the internet all the functions I saw were mainly non-standard. I would appreciate it if someone helped me find functions for this!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > when I looked on the internet all the functions I saw were mainly non-standard.
    All sound and music APIs are non-standard, so you need to say which OS/Compiler you're using if you want more specific advice.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    fopen, fclose, fread, fscanf, fgets, fwrite, fprintf, ...

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by BatchProgrammer View Post
    I do not know any of the functions to do this, and when I looked on the internet all the functions I saw were mainly non-standard. I would appreciate it if someone helped me find functions for this!
    It's non-standard, and it's difficult to play audio. The speaker needs to be fed samples at a constant rate of 1 every 1/44,000 of a second. And the files are often big.
    So you usually have several layers or architecture, with a top-level "play", and lower-level tick functions that manage the flow of samples to buffers, mixers, secondary buffers, and finally the digital to analogue converter.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    55
    Os:

    Microsoft Windows [Version 6.1.7601] ( Windows 7, 64 bit, premium version )

    Compilier: Code::Blocks minGW minimalist package for windows, full version with plugins

    Also, does the C syntax have anyway to handle this then if there is no standard function for it? Or is it more nessecary to have unstandard libraries for such a task? ( I don't like using other people's code to write my programs. )

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The simplest is probably this
    PlaySound function (Windows)

    But windows has several different audio / game / multimedia API's to choose from (and probably several 3rd party ones as well).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    55
    That is C, I don't think it would work in my compilier.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > That is C, I don't think it would work in my compilier.
    Duh!?
    Why the ........ did you title your thread "How do you handle sound and music files in C?" then?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Jan 2013
    Posts
    55
    C++*. Mistyped it. The function on the page was C++.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Guess what? You can use C in a C++ program.

    Sounds like you should spend some more time learning the language(s) before jumping into more complex things like sound and music.

  11. #11
    Registered User
    Join Date
    Jan 2013
    Posts
    55
    I know the C language and syntax, but I do not know C++. Yes, I'm aware that you can use C in C++. I have heard many times though that some C functions in C++ are not recommended for use in C++, not to mention that I've also heard that you have to do something different with the preprocessor directives.

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    You do realize the examples on msdn.microsoft.com normally say C++; can work in most cases in both C and C++.

    This is because the API for MS Windows is almost (if not all) all written in C.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I have heard many times though that some C functions in C++ are not recommended for use in C++
    When you start seeing classes and namespaces in the API, then you know it's a C++ API.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Music in C++ and external files
    By yucaplan in forum C++ Programming
    Replies: 1
    Last Post: 05-20-2005, 11:00 AM
  2. Play music files in C#
    By redintegra in forum C# Programming
    Replies: 1
    Last Post: 04-08-2005, 09:57 AM
  3. Music and sound
    By Shadow12345 in forum Game Programming
    Replies: 7
    Last Post: 04-16-2003, 07:41 PM
  4. Music files to a program
    By y2jasontario in forum C Programming
    Replies: 9
    Last Post: 04-05-2002, 02:22 AM
  5. Can I Load A Sound or Music Into My Win32 Application ?
    By SonicWave in forum Windows Programming
    Replies: 8
    Last Post: 09-21-2001, 07:54 AM