"PlayAudioClip()" Is this a function in C?

Thread Starter

richard3194

Joined Oct 18, 2011
193
Is there a function (in C) that is to do with playing audio clips?
Specifically "PlayAudioClip ()"
I think there might be.
A Google search does not return much.
I'm hoping to read about this function.
Thanks. Rich
 

Thread Starter

richard3194

Joined Oct 18, 2011
193
Anyway, continuing: I see that: "Unity is the award-winning development platform for games and interactive content on the web, iOS, Android, consoles and beyond". But, I might think, what has that got to do with the C function "PlayAudioClip ()". Like, what has "int arrray [2] [3]" got to do with unity.

Anyway, I don't want to get bogged down in conversation that is not helpful and distracting.

So, it seems (to me) that although "PlayAudioClip ()" is a C (actually C#) function, it's tied up with this development platform.
So, I suppose outside of using this development platform, you cannot use the function. Is that correct? Is that the jist of it? Thanks.
 

Thread Starter

richard3194

Joined Oct 18, 2011
193
I'm confused maybe. I've been having a conversation where the hardware is MCU and off-chip memory. And been given some source code that I assumed is C code (pretty sure it's C code) to look at - to get a general hint at working with memory.. Then I've then asked about use of an index with a 2 dimesional array, which goes to the issue of memory (if the data in the array is memory data) - and tends also to bring in the mechanics (in a software sense) of the actual playing of audio, that is, it has brought in "PlayAudioClip ()". Anyhow, I was told to look at the C function "PlayAudioClip ()" and read up on arrays. And now I'm told there is no function of the kind "PlayAudioClip ()" that are part of the standard language. Maybe then it's part of a non standard set of functions? Probably, I suppose (thinking of the platform "unity"), but I'm a newbie. :)

EDIT: I wonder, although the sample code I was given is C code, whether it was written with a special development platform, using standard and non standard C functions. Perhaps MrSoftware could comment if he reads this?
 
Last edited:

Thread Starter

richard3194

Joined Oct 18, 2011
193
Hi. I'm not 100% sure that the source code I was given relates to an SD card (which is OK). There was no file sytem, just raw flash memory. Hard coding used in relation to memory addresses (START - address, LENGTH - bytes).

EDIT: Off to bed, it's 01:28. Just been thrown by PlayAudioClips().

By the way, inside the brackets are the row and column integers of the array (essentially that is)
 
Last edited:

panic mode

Joined Oct 10, 2011
4,867
But, I might think, what has that got to do with the C function "PlayAudioClip ()". Like, what has "int arrray [2] [3]" got to do with unity.
Unity is a game engine. It provides tons of functions specifically developed to handle audio-visual aspects of game programming.
 

Thread Starter

richard3194

Joined Oct 18, 2011
193
Hi. So, it seems the sample code I was given to ponder, to gain some insight as to how to develop C code to play several WAV files (clips), with MCU as the hardware, has been written with a game engine. Because the function PlayAudioClip() is not available otherwise. I did not realise this. I'm not sure whether it's possible to buy a license for non standard C code functions you might want to use and integrate them into a compiler. Is that possible? Thank you.

EDIT: As an aside: Actually I did not even realise PlayAudioClip() was an off-the-shelf or built-in function. For some reason I thought it was something the code writer had made up.

EDIT: I'm not sure there is much mileage in basing a WAV player on this code component PlayAudioClip() . Seems like maybe I'd be better off achieving my project without it. I suspect that may be true. If so, I'd have to find a way without reliance on the function at issue. I'll examine WAV player projects that do not use the function.Rich
 
Last edited:

Thread Starter

richard3194

Joined Oct 18, 2011
193
Hi. What has happened is that I'm wanting to simply play sound clips. And I've taken some quick note of various WAV player projects you can find on the internet. Many of these WAV player projects I think are written in C, and simply modulate a PWM duty cycle to produce the audio. Then, later, it transpired that I was given some sample code and PlayAudioClip() came into focus. Now, that has represented a shift from those simple PWM-based WAV player projects.

I've just recognised that C does not in fact support audio - as such. In the light of these projects, that use a modulated PWM technique, I'm not sure if that suggests I should forget about C, and think about a language that has built in support for audio. I guess that depends on whether the PWM method is acceptable, which it maybe could be.

These projects are MCU based.
 
Last edited:

MrChips

Joined Oct 2, 2009
34,629
You don't have to associate C with audio output. You can do it with any programming language.
Also you do not need WAV nor PWM. There are other ways to save and playback sound.
The most obvious way is to digitize audio with an ADC, save the raw data, and play it back with a DAC. 8-bit audio is not terrible. I have done it with 4-bit log data and even 1-bit delta modulation.
 

Thread Starter

richard3194

Joined Oct 18, 2011
193
Hi. I'm glad I'm having this conversation. I think I'm getting back on track and I'm learning some essential (but basic) stuff. :) Yep, when I take time to think about it, all that is needed is source code that either modulates PWM duty cycle, or simply sends binary data (audio data) to a DAC in the MCU. And any programming language should do that. That represents some progress on my part. For I am a complete newbie.
 

MrSoftware

Joined Oct 29, 2013
2,273
I just now saw this thread. I had responded to a private message from @richard3194 and probably should have made it public right off the bat as it could have been helpful to more people. I had given him some example C snippets to express the general idea for how I was playing audio clips form an MCU. It's not complete code, just fragments to show the general idea. Here is my first message in that thread:

How you store and access your audio data will depend entirely on your use case. In my case the system was embedded and the audio would never change, so I just used the MCU to copy the data to the memory module at programming time, then just read it back as needed, all using memory offsets. If you want to use an actual SD card with a file system so the files can be written to it with a PC, then your MCU will need some sort of file system driver so that it can understand the file system and locate the data using the file name. I hope this helps. :)

Edit --> In the code below, "PlayAudioClip()" would be your own function where you do all the dirty work of actually playing the audio. In my case playing the audio consisted of enabling an audio amplifier and then sending the data over via the amplifiers I2S interface.

Code:
// defines for easier readability
#define AUDIO_ADDRESS 0
#define AUDIO_LENGTH 1

// [Address of audio clip in SPI memory], [size of audio clip in bytes]
// Index into this using AudioClipNames enum values
const int AudioClips[10][2] = {\
                                                             {0x00000, 0x18F16},    \
                                                             {0x19000, 0x126A0},    \
                                                             {0x2B700, 0x19E46},     \
                                                             {0x45600, 0x91E6},    \
                                                             {0x4E800, 0xEC6E},    \
                                                             {0x5D500, 0x121C8},    \
                                                             {0x6F700, 0x18E4C},    \
                                                             {0x88600, 0x1CE86},    \
                                                             {0xA5500, 0x6EA2},    \
                                                             {0xAC400, 0x7B5CA}    \
                                                            };
typedef enum {
    AUDIO_SAY_HELLO= 0,
    AUDIO_SAY_GOODBYE,
    AUDIO.....  ,
}AudioClipNames;

// Then to play a sound
PlayAudioClip(AUDIO_SAY_HELLO);


void PlayAudioClip(int iClipIndex)
{
    int iStartAddress = AudioClips[iClipIndex][AUDIO_ADDRESS];
    int iBytesToRead = AudioClips[iClipIndex][AUDIO_LENGTH];
......
}
 
Last edited:

MrChips

Joined Oct 2, 2009
34,629
There are many ways to skin a cat.

I once made a telephone Caller Display feature that announced the phone number of the caller. If the number was a known number saved in a directory listing, it would announce the name of the caller instead of the phone number.

The audio was my own voice pre-recorded and saved in a WINBOND ISD4004 Voice Recorder chip.
MCU was Motorola MC68HC811. Program was written in ASM.
 

Thread Starter

richard3194

Joined Oct 18, 2011
193
Hi. That code was useful inasmuch as it drew me to look at arrays . So, I know something about arrays and how you index into arrays because after all we have an i index and a j index. I was actually wondering whether there was some other thing, akin to an index, like a table, that the program referred to. But, the thing is, I'm trying to write code for an MCU using PWM or the DAC, which is possible with any kind of code. Apparantly the code above has to be used with UNITY game platform. That was my latest understanding the other day. Which I'm wondering is not necessary/overkill for my project. Unless I understand wrongly. Rich

EDIT: A later understanding: I thought it was told me "PlayAudioClip ()" was not a standard C function (which is true). But one used with UNITY. Perhaps the writer of the above code created "PlayAudioClip ()" and it's just a coincidence that it is the same name as non standard C function in UNITY? I now believe that is the case. Rich
 
Last edited:

MrSoftware

Joined Oct 29, 2013
2,273
The only function built into c is sizeof(). There are probably a thousand functions called PlayAudioClip() floating around, for various uses on various platforms. If you were to use the code above as a general approach, then inside PlayAudioClip is where you would write your code to produce the sounds, or call on other code to help produce the sounds. Whether it be by sending the data to some chip that plays the sound, or by creating your own PWM signal, etc..
 

MrSoftware

Joined Oct 29, 2013
2,273
Read up on the steps of compilation for a C program, this will help you a great deal. Spend some time learning C before you try to tackle the audio thing, it will be much faster and less frustrating in the end. Not just C code syntax, but spend some time understanding how the compiler and linker work. And understand the difference between static and runtime linking, though with micro controllers I suspect you'll use mostly static linking.
 
Top