How to Play MP3 Files in C code using FMOD? - c

I'm trying to play a simple file in my c code and so I looked up how to do it on Stack Overflow, but when I tried to apply the answer, which was to download FMOD and use it, I got an error.
The code was:
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <conio.h>
#include <inc/fmod.h>
FSOUND_SAMPLE* handle;
int main()
{
// init FMOD sound system
FSOUND_Init (44100, 32, 0);
// load and play mp3
handle=FSOUND_Sample_Load (0,"mp3.file",0, 0, 0);
FSOUND_PlaySound (0,handle);
// wait until the users hits a key to end the app
while (!_kbhit())
{
}
// clean up
FSOUND_Sample_Free (handle);
FSOUND_Close();
}
but the error told me that:
"gcc -Wall -c "Cases.c" (in directory: C:\Users\xeobi\Documents\C Files)
Cases.c:5:10: fatal error: inc/fmod.h: No such file or directory
5 | #include <inc/fmod.h>
| ^~~~~~~~~~~~
compilation terminated.
Compilation failed."

Related

Where is random.h in msys2

I am using MSYS2 mingw 64 when compiling code that needs the header random.h I am trying to make that code work on both Linux and windows with the least amount of changes
#include <sys/random.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
srand(time(NULL));
return 0;
}
I ran this command pacman -S msys2-runtime-devel to download the random.h header file and it is located in sys official link
on linux, the file is included using #include <linux/random.c> but I don't know what to use on windows or if I have to do something completely different
When I comment the first line I get this warning
main.c:10:9: warning: implicit declaration of function 'srand' [-Wimplicit-function-declaration]
10 | srand(time(NULL));
| ^~~~~
As per the linked documentation,
srand is declared in #include <stdlib.h>.
rand is declared in #include <stdlib.h>.
Neither requires including random.h or linux/random.c.

How to play sound effect or Music in C?

I am making a game and I have to add some sounds effects and Music.
I Googled it and I found The flowing Code:
#include <conio.h>
#include "inc/fmod.h"
FSOUND_SAMPLE* handle;
int main ()
{
// init FMOD sound system
FSOUND_Init (44100, 32, 0);
// load and play mp3
handle=FSOUND_Sample_Load (0,"my.mp3",0, 0, 0);
FSOUND_PlaySound (0,handle);
// wait until the users hits a key to end the app
while (!_kbhit())
{
}
// clean up
FSOUND_Sample_Free (handle);
FSOUND_Close();
}
But when I compile it I got the flowing error:
➜ Desktop gcc main.c
main.c:1:10: fatal error: 'conio.h' file not found
#include <conio.h>
^~~~~~~~~
1 error generated.
Well, firstly <conio.h> is a C++ library and you're programming in C. It's different!
Then, I remember a C code I wrote years ago, main.c has got the following code (comments are in italian because I am italian):
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "header.h"
int main(){
register unsigned char x='2';
printf("digitare tasti:\n");
while(1){
while(1){
if(x=='2'){/*blocco2*/ while(x!='1' && x!='3'){x=getch(); scala2(x);}}
if(x=='1'){/*blocco1*/ while(x!='2' && x!='3'){x=getch(); scala1(x);}}
if(x=='3'){/*blocco3*/ while(x!='1' && x!='2'){x=getch(); scala3(x);}}
}
}
system("PAUSE");
return 0;
}
Then, this is the other source file, called file.c:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "header.h"
void scala1(unsigned char x){
if(x=='a')beep(131,50);
if(x=='s')beep(147,50);
if(x=='d')beep(165,50);
if(x=='f')beep(175,50);
if(x=='g')beep(196,50);
if(x=='h')beep(220,50);
if(x=='j')beep(247,50);
if(x=='k')beep(262,50);
if(x=='l')beep(294,50);
if(x=='w')beep(139,50);
if(x=='e')beep(156,50);
if(x=='r')beep(185,50);
if(x=='t')beep(208,50);
if(x=='y')beep(233,50);
}
void scala2(unsigned char x){
if(x=='a')beep(262,50);
if(x=='s')beep(294,50);
if(x=='d')beep(330,50);
if(x=='f')beep(349,50);
if(x=='g')beep(392,50);
if(x=='h')beep(440,50);
if(x=='j')beep(494,50);
if(x=='k')beep(523,50);
if(x=='l')beep(587,50);
if(x=='w')beep(277,50);
if(x=='e')beep(311,50);
if(x=='r')beep(370,50);
if(x=='t')beep(415,50);
if(x=='y')beep(466,50);
}
void scala3(unsigned char x){
if(x=='a')beep(523,50);
if(x=='s')beep(587,50);
if(x=='d')beep(659,50);
if(x=='f')beep(698,50);
if(x=='g')beep(784,50);
if(x=='h')beep(880,50);
if(x=='j')beep(988,50);
if(x=='k')beep(1046,50);
if(x=='l')beep(1175,50);
if(x=='w')beep(554,50);
if(x=='e')beep(622,50);
if(x=='r')beep(740,50);
if(x=='t')beep(831,50);
if(x=='y')beep(932,50);
}
The last one, the file header.h. It's code is the following one:
void scala1(unsigned char x);
void scala2(unsigned char x);
void scala3(unsigned char x);
All the source files must be in the same directory. You compile main.c and then, you just need to press a,s,d,..y and 1,2,3. Try! It works, of course if you want to change part of the code, you can do. I hope you enjoy my program, it's funny :)

Why does the agwrite function in the cgraph library unexpectedly fail on any config/platform but Win64 release?

I've been trying to get cgraph (https://graphviz.gitlab.io/_pages/pdf/cgraph.pdf) working so I read and write some graph files. I tried writing some very basic code:
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <float.h>
#include <limits.h>
#include <math.h>
#include <memory.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Windows.h>
#include <mysql.h>
#include <graphviz/cgraph.h>
int main() {
FILE *fp = NULL;
fp = fopen("test.dot", "w+");
if (fp == NULL) {
return -1;
}
Agraph_t *g;
g = agopen("test", Agdirected, NULL);
Agnode_t *signal1;
signal1 = agnode(g, "Signal1_ON", TRUE);
Agnode_t *signal2;
signal2 = agnode(g, "Signal1_OFF", TRUE);
Agedge_t *link = agedge(g, signal1, signal2, "link1", TRUE);
agattr(g, AGEDGE, "label", "transitionlink");
agwrite(g, fp);
fclose(fp);
system("pause");
return 0;
}
What should be happening is that the file should be written to test.dot. This code works perfectly fine on Win64 release, but fails on Win64 debug, Win32 debug, and Win32 release. I have double checked the .lib files and .dll files settings in visual studio and in the file directories, making sure to copy the release and debug versions of each platform correctly. However, the agwrite keeps causing a "Microsoft Visual Studio C Runtime Library has detected a fatal error" crash on Win64 debug, Win32 debug, and Win32 release. The weird thing is if I change
agwrite(g, fp); to agwrite(g, stdout);, the code works on all platforms/configurations. I am so confused why this is happening. Here is the source file which contains the code for agwrite if that helps: https://github.com/ellson/MOTHBALLED-graphviz/blob/master/lib/cgraph/write.c
I cannot debug the issue because the source has been compiled into .dlls, and .libs for each platform/configuration.
I appreciate any suggestions/feedback,
Thank you
Edit:
For anyone godly enough to try and get this working on their own system, here are all my binaries, libs, and include files: https://www.dropbox.com/sh/o9tjz7txu4m0k5q/AAAnp8Wu99q9IsFN7kvqZP7Ta?dl=0
Edit 2:
The compiler I am using is MSVC 14 on Windows 10.
I found out that using cgraph directly results in an error when trying to use agwrite(). The solution is to use the GVC abstraction layer which comes with the Graphviz C API to do file I/O. Here is the code that worked:
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <float.h>
#include <limits.h>
#include <math.h>
#include <memory.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Windows.h>
#include <mysql.h>
#include <graphviz/gvc.h>
int main() {
GVC_t *gvc;
gvc = gvContext();
Agraph_t *g;
g = agopen("test", Agdirected, NULL);
Agnode_t *signal1;
signal1 = agnode(g, "Signal1_ON", TRUE);
Agnode_t *signal2;
signal2 = agnode(g, "Signal1_OFF", TRUE);
Agedge_t *link = agedge(g, signal1, signal2, "link1", TRUE);
agattr(g, AGEDGE, "label", "transitionlink");
gvLayout(gvc, g, "dot");
gvRenderFilename(gvc, g, "dot", "test.dot");
gvFreeLayout(gvc, g);
agclose(g);
gvFreeContext(gvc);
system("pause");
return 0;
}
Edit:
Here is the documentation for GVC: https://graphviz.gitlab.io/_pages/pdf/gvc.3.pdf
The reason of crashing is described on official Graphviz site:
This usually happens when the Graphviz library is built using one version of the stdio library, and the user’s program is compiled using another. If the FILE structure of stdio is different, the call to agread() will cause a crash. This is mainly a problem on Windows where we just provide a binary release built with one version of Visual Studio and stdio changes depending on the version of Visual Studio. It can also occur if the user tries to use cygwin or something similar which may also use an incompatible stdio.
https://graphviz.org/faq/#FaqAgreadCrash

libavformat / ffmpeg c program "not suitable input format"

Hello I'm attempting to encode videos using the C ffmpeg libraries. For some reason, when I attempt to create a format context I get an error with:
[NULL # 0x7ff574006600] Requested output format 'mp4' is not a suitable output format
I know that my ffmpeg is compiled with the x264 flags, as a simple
ffmpeg -i test.mov test.mp4
works fine. I'm also linking with all of the generated pch files and everything else from the libav* libraries is working fine. Here's a full working example with the error's I'm recieving:
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/avutil.h>
#include <libswscale/swscale.h>
#include <libavutil/opt.h>
#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>
#include <stdio.h>
int main() {
AVFormatContext * output = NULL;
// now lets initialize the actual format element
if (avformat_alloc_output_context2(&output, NULL, "mp4", "test.mp4") < 0) {
printf("%s", "INVALID");
}
return 0;
}
You have to call
av_register_all();
prior to allocating the output context.

OpenCV242 on eclipse in windows

I want to work with OpenCV2.4.2 . and I tried a simple progam to test if it'll work here is the code :
#include "stdlib.h"
#include "stdio.h"
#include "math.h"
#include "cv.h"
#include "highgui.h"
int main(int argc, char *argv[])
{
// Nothing but create a window
cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
cvMoveWindow("mainWin", 100, 100);
cvWaitKey(0);
return 0;
}
wenn I try to build and run this I get this error message from Eclipse :
fatal error: opencv2/core/core_c.h: No such file or directory
line 63, external location: D:\opencv\include\opencv\cv.h C/C++ Problem
Symbol 'CV_WINDOW_AUTOSIZE' could not be resolved source.c line 18 Semantic Error
Any Idea why it doesn't work ??
Because the compiler does not know the location of opencv headers.

Resources