🔊 As bibliotecas de diversas linguagens de programação são escritas em C e C++. Nesse vídeo veremos como fazer isso em C++ para Python.
Arquivo main.cpp
:
#include <Python.h>
int world(){
printf("Olá, Mundo!\n");
return 0;
}
int add(int x, int y){
return x + y;
}
int print(const char* str){
printf("%s", str);
return 0;
}
static PyObject* world(PyObject* self, PyObject* args){
return Py_BuildValue("s", world());
}
static PyObject* print(PyObject* self, PyObject* args){
const char* z;
if(!PyArg_ParseTuple(args, "s", &z)){
return NULL;
}
return Py_BuildValue("s", print(z));
}
static PyObject* add(PyObject* self, PyObject* args){
int a, b;
if(!PyArg_ParseTuple(args, "ii", &a, &b)){
return NULL;
}
return Py_BuildValue("i", add(a, b));
}
static PyMethodDef hello_methods[] = {
{"world", world, METH_VARARGS, "Print Hello World"},
{"add", add, METH_VARARGS, "Add two numbers."},
{"print", print, METH_VARARGS, "Print string"},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef hello_module = {
PyModuleDef_HEAD_INIT,
"hello",
NULL,
-1,
hello_methods
};
PyMODINIT_FUNC PyInit_hello(void){
return PyModule_Create(&hello_module);
}
Código Python criado:
script.py
import hello
hello.world()
num1 = 36
num2 = 90
print(hello.add(num1, num2))
hello.print("Olá, amigo somente\n")
Compile:
g++ -shared -o hello.so -fpic main.cpp -I /usr/include/python* # Altere pra versão que há no seu sistema
Rode:
python script.py
playmp3
:Exemplo no Ubuntu
sudo apt install libmpg123-dev libao-dev libasound2
playmp3.hpp
e playmp3.cpp
) da postagem do vídeo da playmp3 disponível no endereço:main.cpp
e adicione o código abaixo:#include <Python.h>
#include "playmp3.hpp"
int mp3(char* song){
auto p = std::make_unique<PlayMP3>();
p->music(song);
p->play();
return 0;
}
static PyObject* mp3(PyObject* self, PyObject* args){
char* music;
if (!PyArg_ParseTuple(args, "s", &music)) {
return NULL;
}
mp3(music);
return Py_BuildValue("");
}
static PyMethodDef playmp3_methods[] = {
{"mp3", mp3, METH_VARARGS, "Play MP3"},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef playmp3_module = {
PyModuleDef_HEAD_INIT,
"playmp3",
NULL,
-1,
playmp3_methods
};
PyMODINIT_FUNC PyInit_playmp3(void) {
return PyModule_Create(&playmp3_module);
}
libalsa.so
à variável de ambiente: LD_LIBRARY_PATH
:echo export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$(dirname "$(find /usr/lib* -name "libalsa.so" 2>/dev/null)") >> ~/.bashrc
source ~/.bashrc
g++ -shared -o playmp3.so -fpic main.cpp playmp3.cpp \
-I /usr/include/python* \ # Altere para o caminho que há no seu sistema
-lmpg123 -lao $(find /usr/lib* -name "libalsa.so")
player.py
import playmp3 as play
play.mp3("music.mp3")
Em
music.mp3
substitua pela música que deseja escutar!
python player.py
Se quiser utilizar a biblioteca diretamente no seu sistema:
sudo mv playmp3.so /usr/local/lib
echo 'export PYTHONPATH="${PYTHONPATH}:/usr/local/lib"' >> ~/.bashrc
Se quiser usar um plugin de saída diferente para o
libao
, como o plugin “oss” (Open Sound System), use:
export AO_DRIVER=oss
python player.py