Skip to content

Commit 3c64879

Browse files
rickrick
rick
authored and
rick
committed
add golang cgo bridge examples
1 parent c9c0276 commit 3c64879

28 files changed

+1348
-14
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,6 @@ deps-test.sqlite
2626
sqldb-test/sqldb-test.sqlite
2727
loc.sh
2828
.Kitty-logo*
29+
*/*.a
30+
*/*.mod
31+
*/*.sum

.gitmodules

+6
Original file line numberDiff line numberDiff line change
@@ -550,3 +550,9 @@
550550
[submodule "submodules/hidapi"]
551551
path = submodules/hidapi
552552
url = https://github.com/libusb/hidapi
553+
[submodule "submodules/miniaudio"]
554+
path = submodules/miniaudio
555+
url = https://github.com/mackron/miniaudio
556+
[submodule "submodules/c89atomic"]
557+
path = submodules/c89atomic
558+
url = https://github.com/mackron/c89atomic

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ git-pull:
8989
do-uncrustify: uncrustify uncrustify-clean fix-dbg
9090
do-build: do-meson
9191
@meson compile -C build
92+
@meson install -C build --tags build
9293
do-test:
9394
@passh meson test -C build -v --print-errorlogs
9495
test: do-test

deps-test/deps-test.c

+187-7
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,42 @@
11
#define MKCREFLECT_IMPL
22
#define LAY_IMPLEMENTATION
3+
#define MINIAUDIO_IMPLEMENTATION
34
////////////////////////////////////////////
4-
#include <wchar.h>
55
#include <assert.h>
66
#include <err.h>
77
#include <errno.h>
88
#include <inttypes.h>
9+
#include <libgen.h>
10+
#include <limits.h>
911
#include <math.h>
1012
#include <poll.h>
1113
#include <stdio.h>
12-
#include <stdio.h>
1314
#include <stdlib.h>
14-
#include <unistd.h>
1515
#include <string.h>
1616
#include <sys/socket.h>
17+
#include <sys/stat.h>
1718
#include <unistd.h>
19+
#include <unistd.h>
20+
#include <wchar.h>
1821
////////////////////////////////////////////
22+
#include "c89atomic/c89atomic.h"
1923
#include "deps-test/deps-test.h"
2024
#include "generic-print/print.h"
21-
#include "layout/layout.h"
22-
#include "libforks/libforks.h"
23-
#include "log.h/log.h"
24-
#include "tempdir.c/tempdir.h"
2525
#include "hidapi/hidapi/hidapi.h"
2626
#include "hidapi/mac/hidapi_darwin.h"
27+
#include "layout/layout.h"
28+
#include "libforks/libforks.h"
2729
#include "libusb/libusb/libusb.h"
2830
#include "libusb/libusb/os/darwin_usb.h"
31+
#include "log.h/log.h"
32+
#include "miniaudio/miniaudio.h"
33+
#include "tempdir.c/tempdir.h"
2934
////////////////////////////////////////////
3035
static int do_get_google();
3136
static inline int file_exists(const char *path);
3237

38+
static char *EXECUTABLE_PATH_DIRNAME;
39+
3340
#define DEF_PORT 8080
3441
#define MAX_NAME 40
3542
static int port = DEF_PORT;
@@ -1638,6 +1645,138 @@ TEST t_libusb2(void){
16381645
}
16391646

16401647

1648+
void record_callback(ma_device *pDevice, void *pOutput, const void *pInput, ma_uint32 frameCount){
1649+
ma_encoder *pEncoder = (ma_encoder *)pDevice->pUserData;
1650+
1651+
MA_ASSERT(pEncoder != NULL);
1652+
ma_encoder_write_pcm_frames(pEncoder, pInput, frameCount, NULL);
1653+
(void)pOutput;
1654+
}
1655+
1656+
1657+
void play_callback(ma_device *pDevice, void *pOutput, const void *pInput, ma_uint32 frameCount){
1658+
ma_decoder *pDecoder = (ma_decoder *)pDevice->pUserData;
1659+
1660+
if (pDecoder == NULL) {
1661+
return;
1662+
}
1663+
1664+
ma_decoder_read_pcm_frames(pDecoder, pOutput, frameCount, NULL);
1665+
1666+
(void)pInput;
1667+
}
1668+
1669+
1670+
static char *get_self_path(void){
1671+
char dir[PATH_MAX];
1672+
uint32_t size = sizeof dir;
1673+
1674+
_NSGetExecutablePath(dir, &size);
1675+
return(dir);
1676+
}
1677+
1678+
1679+
int do_miniaudio_record_file(char *record_file){
1680+
ma_result result;
1681+
ma_encoder_config encoderConfig;
1682+
ma_encoder encoder;
1683+
ma_device_config deviceConfig;
1684+
ma_device device;
1685+
1686+
encoderConfig = ma_encoder_config_init(ma_encoding_format_wav, ma_format_f32, 2, 44100);
1687+
1688+
if (ma_encoder_init_file(record_file, &encoderConfig, &encoder) != MA_SUCCESS) {
1689+
printf("Failed to initialize output file.\n");
1690+
return(-1);
1691+
}
1692+
1693+
deviceConfig = ma_device_config_init(ma_device_type_capture);
1694+
deviceConfig.capture.format = encoder.config.format;
1695+
deviceConfig.capture.channels = encoder.config.channels;
1696+
deviceConfig.sampleRate = encoder.config.sampleRate;
1697+
deviceConfig.dataCallback = record_callback;
1698+
deviceConfig.pUserData = &encoder;
1699+
1700+
result = ma_device_init(NULL, &deviceConfig, &device);
1701+
if (result != MA_SUCCESS) {
1702+
printf("Failed to initialize capture device.\n");
1703+
return(-2);
1704+
}
1705+
1706+
result = ma_device_start(&device);
1707+
if (result != MA_SUCCESS) {
1708+
ma_device_uninit(&device);
1709+
printf("Failed to start device.\n");
1710+
return(-3);
1711+
}
1712+
1713+
usleep(1000 * 1000);
1714+
1715+
ma_device_uninit(&device);
1716+
ma_encoder_uninit(&encoder);
1717+
1718+
return(0);
1719+
} /* do_miniaudio_record_file */
1720+
1721+
1722+
int do_miniaudio_play_file(char *wav_file){
1723+
ma_result result;
1724+
ma_engine engine;
1725+
1726+
result = ma_engine_init(NULL, &engine);
1727+
if (result != MA_SUCCESS) {
1728+
printf("Failed to initialize audio engine.");
1729+
return(-1);
1730+
}
1731+
1732+
1733+
float len = 0;
1734+
1735+
// ma_sound_get_cursor_in_seconds(&engine,&len);
1736+
printf("ma_sound_get_cursor_in_seconds:%f\n", len);
1737+
1738+
ma_sound sound;
1739+
1740+
result = ma_sound_init_from_file(&engine, wav_file, 0, NULL, NULL, &sound);
1741+
if (result != MA_SUCCESS) {
1742+
return(result);
1743+
}
1744+
1745+
//ma_sound_set_stop_time_in_pcm_frames(&sound, ma_engine_get_time(&engine) + (ma_engine_get_sample_rate(&engine) * 2));
1746+
1747+
1748+
ma_sound_start(&sound);
1749+
1750+
//ma_engine_play_sound(&engine, wav_file, NULL);
1751+
1752+
usleep(1000 * 1000);
1753+
1754+
1755+
ma_engine_uninit(&engine);
1756+
return(0);
1757+
}
1758+
1759+
1760+
TEST t_miniaudio_play_file(void *PLAY_FILE){
1761+
int res;
1762+
1763+
res = do_miniaudio_play_file((char *)PLAY_FILE);
1764+
ASSERT_EQ(res, 0);
1765+
printf("play ok- %s\n", (char *)PLAY_FILE);
1766+
PASS();
1767+
}
1768+
1769+
1770+
TEST t_miniaudio_record_file(void *RECORD_FILE){
1771+
int res;
1772+
1773+
res = do_miniaudio_record_file((char *)RECORD_FILE);
1774+
ASSERT_EQ(res, 0);
1775+
printf("recorded ok- %s\n", (char *)RECORD_FILE);
1776+
PASS();
1777+
}
1778+
1779+
16411780
TEST t_tempdir(void){
16421781
char *temp_dir = gettempdir();
16431782

@@ -1667,9 +1806,43 @@ TEST t_regex(void){
16671806
if (match_idx >= 0) {
16681807
printf("match at idx %i, %i chars long.\n", match_idx, match_length);
16691808
}
1809+
PASS();
1810+
}
1811+
1812+
1813+
TEST t_c89atomic(void){
1814+
c89atomic_flag a0 = 0;
1815+
c89atomic_flag b0 = 1;
1816+
c89atomic_bool r0 = c89atomic_flag_test_and_set(&b0);
1817+
1818+
PRINT("r0:", r0);
1819+
1820+
c89atomic_uint8 a = 42;
1821+
c89atomic_uint8 b = 123;
1822+
1823+
c89atomic_store_8(&a, b);
1824+
1825+
PRINT("a:", a);
1826+
PRINT("b:", b);
16701827

16711828
PASS();
16721829
}
1830+
1831+
SUITE(s_c89atomic) {
1832+
RUN_TEST(t_c89atomic);
1833+
}
1834+
1835+
SUITE(s_miniaudio) {
1836+
char play_file[1024];
1837+
1838+
sprintf(&play_file, "%s/../sounds/key_space_down.wav", EXECUTABLE_PATH_DIRNAME);
1839+
char record_file[1024] = "/tmp/record.wav";
1840+
1841+
RUN_TESTp(t_miniaudio_play_file, (void *)play_file);
1842+
RUN_TESTp(t_miniaudio_record_file, (void *)record_file);
1843+
RUN_TESTp(t_miniaudio_play_file, (void *)record_file);
1844+
}
1845+
16731846
SUITE(s_hidapi) {
16741847
RUN_TEST(t_hidapi);
16751848
}
@@ -1716,6 +1889,10 @@ GREATEST_MAIN_DEFS();
17161889

17171890

17181891
int main(int argc, char **argv) {
1892+
char EXECUTABLE_PATH[PATH_MAX + 1] = { 0 };
1893+
1894+
realpath(argv[0], EXECUTABLE_PATH);
1895+
EXECUTABLE_PATH_DIRNAME = dirname(EXECUTABLE_PATH);
17191896
GREATEST_MAIN_BEGIN();
17201897
RUN_SUITE(s_json);
17211898
RUN_SUITE(s_string);
@@ -1751,8 +1928,11 @@ int main(int argc, char **argv) {
17511928
RUN_SUITE(s_tempdir);
17521929
RUN_SUITE(s_hidapi);
17531930
RUN_SUITE(s_libusb);
1931+
RUN_SUITE(s_miniaudio);
1932+
RUN_SUITE(s_c89atomic);
17541933
GREATEST_MAIN_END();
17551934
size_t used = do_dmt_summary();
1935+
17561936
dbg(used, %lu);
17571937
assert(used == 0);
17581938
} /* main */

deps-test/meson.build

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ deps_include_dirs = [
77
deps_test_deps = [
88
hidapi_dep,
99
libusb_dep,
10+
miniaudio_dep,
11+
c89atomic_dep,
1012
tempdir_dep,
1113
deps_dep,
1214
occurrences_dep,
@@ -51,6 +53,7 @@ deps_c_args = [
5153
]
5254
deps_link_args = [
5355
]
56+
5457
if get_option('enable-binaries')
5558
deps_test_exec = executable('deps-test',
5659
deps_test_srcs,

etc/sounds/key_space_down.wav

44.5 KB
Binary file not shown.

meson.build

+21-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
project('meson_deps',['c'],default_options:['warning_level=0','c_std=gnu99'])
2+
#############################################################
3+
go_program = find_program('go', required : true)
4+
gofmt_program = find_program('gofmt', required : true)
5+
goimports_program = find_program('goimports', required : true)
6+
unlink_program = find_program('unlink', required : true)
27
passh_program = find_program('passh', required : true)
38
sqlite3_program = find_program('sqlite3', required : true)
49
sh_program = find_program('sh', required : true)
@@ -7,25 +12,28 @@ passh_program = find_program('passh', required : true)
712
timeout_bin = find_program('timeout', required : true)
813
sudo_bin = find_program('sudo', required : true)
914
git = find_program('git', required: true)
10-
15+
#############################################################
1116
fs = import('fs')
12-
13-
if git.found() and fs.is_dir('.git')
17+
cc = meson.get_compiler('c')
18+
meson_deps_dir = meson.current_source_dir()
19+
#############################################################
20+
if fs.is_dir('.git')
1421
git_rev_parse = run_command(git, 'rev-parse', '--short', '@', check: true)
1522
git_sha = git_rev_parse.stdout().strip()
1623
else
1724
git_sha = ''
1825
endif
19-
26+
go_version = run_command(go_program,'version', check:true).stdout().strip().split(' ')[2].replace('go','')
27+
#############################################################
2028
version_info = configuration_data()
2129
version_info.set('version', meson.project_version())
30+
version_info.set('go_version', go_version)
2231
version_info.set('vcs_tag', git_sha)
23-
32+
#############################################################
2433
summary({'version': meson.project_version(),
2534
'vcs_tag': git_sha,
35+
'go_version': go_version,
2636
}, section: 'Options1')
27-
cc = meson.get_compiler('c')
28-
meson_deps_dir = meson.current_source_dir()
2937
pwd = meson_deps_dir
3038
#############################################################
3139
inc = [
@@ -51,11 +59,17 @@ inc = [
5159
include_directories('submodules/sqxclib/sqxc'),
5260
]
5361
###########################################
62+
install_subdir('etc/sounds', install_dir: meson.current_build_dir(), install_tag:'build')
63+
###########################################
5464
subdir('meson')
5565
subdir('submodules')
5666
#subdir('meson/deps/libtermkey')
5767
###########################################
5868
subdir('deps')
69+
subdir('select_description1')
70+
subdir('multi_select')
71+
subdir('multi_select0')
72+
subdir('multi_select1')
5973
subdir('ctable')
6074
subdir('tests-table')
6175
subdir('introspect')

meson/deps/c89atomic/meson.build

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
c89atomic_srcs = [files(
2+
'../../../submodules/c89atomic/c89atomic.h',
3+
)]
4+
5+
c89atomic_dirs = [
6+
'../../../submodules/c89atomic',
7+
inc,
8+
]
9+
10+
c89atomic_deps = [
11+
12+
]
13+
14+
c89atomic_lib = library('c89atomic',
15+
c89atomic_srcs,
16+
install: false,
17+
dependencies: c89atomic_deps,
18+
include_directories: c89atomic_dirs,
19+
)
20+
21+
c89atomic_dep = declare_dependency(
22+
include_directories: c89atomic_dirs,
23+
link_with: c89atomic_lib,
24+
)

0 commit comments

Comments
 (0)