How to use FMOD with C++? - c++

I'm trying to create a simple mp3 player using FMOD:
#include "inc/fmod.h"
int main()
{
FSOUND_Init(44100, 32, 0);
return 0;
}
Trying to compile the program I get the following error:
holle#x300:justmp3$ pwd
/media/daten/Entwicklung/C/justmp3
holle#x300:justmp3$ LD_LIBRARY_PATH=$(pwd)/lib
holle#x300:justmp3$ ls $LD_LIBRARY_PATH
libfmodex-4.34.02.so libfmodexL-4.34.02.so
holle#x300:justmp3$ g++ -o mp3 mp3.cpp
mp3.cpp: In function ‘int main()’:
mp3.cpp:8: error: ‘FSOUND_Init’ was not declared in this scope
What's my mistake? How could I get g++ to compile the program?

FSOUND_Init is an FMOD 3 API function, you are using FMOD Ex so that function doesn't exist. To initialize FMOD Ex you should include "fmod.hpp" and use the functions:
System_Create to create the FMOD system object, then
System::init to initialize, followed by
System::createStream to load your MP3, then
System::playSound to play it.
There are a bunch of useful examples that ship with FMOD that you could use as a reference, especially the playstream example for what you want to achieve. Also there is full documentation in CHM format. If you are porting code from FMOD 3 I would recommend reading the migration guide in the fmodex.chm docs.

You need to include the headers for the library too, add
#include <fmod.h>
at the beginning of your code.

Related

SystemC: read() and write() to port doesnt work

I am new to systemC It might look stupid but I would appreciate help.
In the below code
In main function :write() to aa and bb shows value 0 when read using aa.read() and bb.read() which should be 10 and 20
And also I think it should enter the method do_add() in the adder module as it is sensitive to a and b and a,b are binded to aa and bb signals but it doesn't call the method do_add(). How does it work and is there any error in the code?
For compiling the code:
g++ -I. -I$SYSTEMC_HOME/include -L. -L$SYSTEMC_HOME/lib-linux64 -Wl,-rpath=$SYSTEMC_HOME/lib-linux64 -o out adder.cpp -lsystemc -lm
./out
#include "systemc.h"
#define WIDTH 32
SC_MODULE(adder) {
sc_in<sc_uint<WIDTH> > a, b;
sc_out<sc_uint<WIDTH> > sum;
void do_add() {
// cout<<"hello"<<endl;
// cout<<a.read()<<b.read()<<"\n";
sum.write(a.read() + b.read());
// cout<<sum.read()<<endl;
}
SC_CTOR(adder) {
SC_METHOD(do_add);
sensitive << a << b;
}
};
int sc_main(int argc, char* argv[]) {
sc_signal<sc_uint<WIDTH> >aa,bb;
adder add("Adder");
add.a(aa);
add.b(bb);
aa.write(10);
bb.write(20);
cout<<aa.read()<<bb.read()<<"\n";
}
There are a number of things here. Firstly, you never actually start the simulation (see function sc_start) and even if you did it would exit immediately since there are no events to process. And lastly, even if you fixed those issues you would still get the same result since SystemC simulates hardware and reading a port in the same delta cycle as it was written will give you the original value on the port not the newly written one. How can hardware change the value of a signal in zero time?
The best advice I can give is to look in the directory where you downloaded SystemC and you will see a directory of PDF documents. On my SystemC it is in a directory called 'docs' (it may be different on yours depending on who installed it and where). If you look in there you will find a 'user guide' and some other PDF documents. These explain how SystemC works and give you examples you can try and modify to your own needs. Also there are some examples in the SystemC sources in the 'examples' folder. You can try playing around with these to get a feel for it and maybe just cut and paste some code and modify it to get what you require.

Call mex Function from cmex SFunction

I have implemented a complex mexFunction using visual studio 2012 and successfully integrated it with Matlab. (lets call it mexFunctionA.mexw32 )
When I run this command in matlab command window, I get the expected results:
mexFunctionA("My1Argument", "My2Argument");
Now , I need to develop a mexFunctionB that calls mexFunctionA; mexFunctionB is a simple as it can be.
The C code I´m trying (inside mexFunctionB.c) is:
#include "mexFunctionA.mexw32"
(...)
static void mdlOutputs(SimStruct *S, int_T tid)
{
mexFunctionA("My1Argument", "My2Argument");
}
(...)
This line of code is not compiling.
The command line I am using is:
mex -v mexFunctionB.c -I'C:\patchToMexFunctionA' -L'C:\patchToMexFunctionA' 'mexFunctionA.mexw32'
So, here are the possible errors:
The #include method is wrong.
The command line for compiling the code is wrong.
It is not possible to do what I am planning to do.
Something else.
Anyone knows how to fix it?
The code you give is non-sensical. .mexw32 files are dynamically linked libraries (i.e. dll's), and in C code #include statements aren't used to include dll's.
Firstly note that as far as your S-Function is concerned mexFunctionA is no different from any other MATLAB function. So the question you should be asking is "how do I call a MATLAB function from within a mex file?".
The answer to that is to use the function mexCallMATLAB.
In short, you need to remove the #include and reformat the call to mexFunctionA to the form required by mexCallMATLAB.

Inputting values from .mat file to array in C++

I am trying to copy a 1100x1100 matrix from a .mat file to an array variable of type float in C++. I read online and found that the matio library is a good option. I installed their library using "make" on Ubuntu 12.04 (I followed the method given on their webpage).
However, I am unable to write code using it mainly because I am new to C++. I am using g++ to compile the file. I get errors such as "unknown reference to Mat_Open" and so on.
I did find this bit of code on the webpage:
#include <stdlib.h>
#include <stdio.h>
#include "matio.h"
int main(int argc,char **argv)
{
mat_t *matfp;
matvar_t *matvar;
matfp = Mat_Open(argv[1],MAT_ACC_RDONLY); //here argv[1] is "a.mat"?
if ( NULL == matfp )
{
fprintf(stderr,"Error opening MAT file %s0,argv[1]);
return EXIT_FAILURE;
}
matvar = Mat_VarReadInfo(matfp,"x"); // x is the variable we are trying to access?
if ( NULL == matvar )
{
fprintf(stderr,"Variable ’x’ not found, or error reading MAT file\n");
}
I have a couple of questions:
here, argv[1] corresponds to the .mat file I am trying to open right?
x in this code is the variable present in the .mat file I am trying to copy?
When I ran this code, I received errors stating - Unknown reference to Mat_Open and so on. Another couple of the same type of errors also were there.
I compiled this using : g++ abc.cpp -o test. (Followed by ./test. But I never got around to that due to the errors obtained during compilation).
How can I make it work? Is there any mistake with the code I used? Or with the compile statement I am using-maybe there are some linkers I need to use for compilation.
Thank you. Please remember that I am new to C++. Any advice would be helpful.
1) argv[1] - is a first parameter you put after your program call. If you want to "feel it", use debugger or code like this:
#include <iostream>
for (unsigned i = 0; i < argc; ++i)
{
std::cout << argv[i] << std::endl;
}
2) Yes, looking at http://libmatio.sourcearchive.com/documentation/1.3.4/group__MAT_g4c8205ff25c5b688a40775fbb1840b7e.html I can say, that you will read variable with name "x".
3) "undefined reference" means you need to build with matio libraries. Add something like "-lLibraryName" to your compile string. And it will have to be built.
To avoid many problems, try to install Code::Blocks, it's cross-platform and quite easy to start using C++ if you never did it before. It also supports debuggers, so you will avoid many problems quite easy.

Is there a way to get a Unix timestamp in Stata?

I searched online and found several sources that talk about converting Unix timestamps to various workable formats, but none that allow me to actually get such a timestamp from within Stata. As of now, I use variations on
local curr_date = c(current_date)
local curr_time = c(current_time)
to apply timestamps to logs, data sets, etc. but I'd like to just use the Unix timestamp in seconds, if possible.
Are you familiar with help datetime? My understanding is that the Unix time stamp would be something like
display %12.0g clock("`c(current_date)' `c(current_time)'", "DMY hms" )/1000 - clock("1 Jan 1970", "DMY" )/1000
which of course you can use in other circumstances as well. (I am not a C programmer, I am a Stata user, but I do understand that it is easier for most people on this site to write a snippet of C code that would go into the guts of Stata than to RTFM... which is admirable in its own ways from where I sit, of course.)
One way to achieve this is to write a simple plugin. Compile this code, in a file called unixtimestamp.c
#include "stplugin.h"
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
STDLL stata_call(int argc, char *argv[])
{
time_t seconds;
seconds = time(NULL);
char buf[33];
sprintf(buf, "%d", seconds);
SF_display(buf);
return(0);
}
with stplugin.h and stplugin.c in the same directory using this command (for a Linux system):
gcc -O3 -shared -DSYSTEM=OPUNIX -fPIC stplugin.c unix_timestamp.c -o unixtimestamp.plugin
The guide to creating plugins uses this command:
gcc -shared -DSYSTEM=OPUNIX stplugin.c unixtimestamp.c -o unixtimestamp.plugin
but on some systems, this gives an error instructing you to use the -fPIC flag, which is why I include it in my command. Also, optimizations aren't really necessary for such a simple plugin, but I included them regardless.
unixtimestamp.plugin should be placed in the ado/personal/ directory. Run Stata's sysdir function to find its location. On my system, it's HOME/ado/personal/ so I copied the plugin there. Then, from Stata, load the plugin:
program unixtimestamp, plugin
If no error message is displayed, run the plugin with:
plugin call unixtimestamp
As with any Stata command, you can also use a macro to simplify this if you plan to use this command frequently:
local unixtime plugin call unixtimestamp
`unixtime'
I use the following to grab a date/time stamp:
di "DateTime: $S_DATE $S_TIME"
Updated:
You may use Unix shell commands directly inside Stata by prefixing them an exclamation mark. To echo Unix time try:
!date +%s
1344341160

"??? Undefined function or method" in Matlab Engine command window

I am working with Visual Studio on C++ code, and I send some variables to MATLAB engine. So far there is no problem. Imagine I have the MATLAB command window opened and these variables:
» whos
Name Size Bytes Class Attributes
QWe 1x365 2920 double
QWp 1x364 2912 double
QWu 1x364 2912 double
I can use the standard MATLAB functions, but I have dowloaded a new function.m (which works in MATLAB normally after setting the path) that this command window from MATLAB's engine is not recognizing or finding.
» isnormq(Q)
??? Undefined function or method 'isnormq'
I thought they (command window and MATLAB) were synchronized but now I see they aren't.
How can I solve this so I can use my function.m from the command window? Any help will be welcomed.
As it has been a long time and nobody answered I will post what I did:
In Matlab everytime you need a newtoolbox you link the path once and there stays "forever". You can use functions from the toolbox as if they were by default in Matlab.
Using matlab engine this doesn't work that way so it is necessary to write the command line:
% Here we load the toolbox for converting quaternion to Euler Angles
addpath(genpath('C:\Program Files (x86)\MATLAB\R2010a\toolbox\SpinCalc')); //for example
It's been a long time but I face the same problem and find some interesting information about it.
First of all at the newer version of MATLAB (2016a) MATLAB Engine search path is C:\Program Files\MATLAB\R2016a (Or the same path where you install MATLAB). So if you don't change it C++ can use all the build-in functions. Furthermore, you can using functions from Toolboxes!
But there is another one problem: what about user defined functions? So I need to put .m file directly to C:\Program Files\MATLAB\R2016a to make it visible for MATLAB Engine.
Here we can go another way - just add the path of your .m file to MATLAB through C++:
char CommandChangePath[MAX_PATH];
strcpy(CommandChangePath, "addpath('C:\\Users\\SuperUser\\Documents\\Visual Studio 2017\\Projects\\MyCppProject')");
engEvalString(ep, CommandChangePath);
For me its very useful to put necessary MATLAB function in current C++ project, add the path and use then! Now you don't need to change the path at every step - it's remembered and useful always for current application.