|
1 | 1 | #define MKCREFLECT_IMPL
|
2 | 2 | #define LAY_IMPLEMENTATION
|
| 3 | +#define MINIAUDIO_IMPLEMENTATION |
3 | 4 | ////////////////////////////////////////////
|
4 |
| -#include <wchar.h> |
5 | 5 | #include <assert.h>
|
6 | 6 | #include <err.h>
|
7 | 7 | #include <errno.h>
|
8 | 8 | #include <inttypes.h>
|
| 9 | +#include <libgen.h> |
| 10 | +#include <limits.h> |
9 | 11 | #include <math.h>
|
10 | 12 | #include <poll.h>
|
11 | 13 | #include <stdio.h>
|
12 |
| -#include <stdio.h> |
13 | 14 | #include <stdlib.h>
|
14 |
| -#include <unistd.h> |
15 | 15 | #include <string.h>
|
16 | 16 | #include <sys/socket.h>
|
| 17 | +#include <sys/stat.h> |
17 | 18 | #include <unistd.h>
|
| 19 | +#include <unistd.h> |
| 20 | +#include <wchar.h> |
18 | 21 | ////////////////////////////////////////////
|
| 22 | +#include "c89atomic/c89atomic.h" |
19 | 23 | #include "deps-test/deps-test.h"
|
20 | 24 | #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" |
25 | 25 | #include "hidapi/hidapi/hidapi.h"
|
26 | 26 | #include "hidapi/mac/hidapi_darwin.h"
|
| 27 | +#include "layout/layout.h" |
| 28 | +#include "libforks/libforks.h" |
27 | 29 | #include "libusb/libusb/libusb.h"
|
28 | 30 | #include "libusb/libusb/os/darwin_usb.h"
|
| 31 | +#include "log.h/log.h" |
| 32 | +#include "miniaudio/miniaudio.h" |
| 33 | +#include "tempdir.c/tempdir.h" |
29 | 34 | ////////////////////////////////////////////
|
30 | 35 | static int do_get_google();
|
31 | 36 | static inline int file_exists(const char *path);
|
32 | 37 |
|
| 38 | +static char *EXECUTABLE_PATH_DIRNAME; |
| 39 | + |
33 | 40 | #define DEF_PORT 8080
|
34 | 41 | #define MAX_NAME 40
|
35 | 42 | static int port = DEF_PORT;
|
@@ -1638,6 +1645,138 @@ TEST t_libusb2(void){
|
1638 | 1645 | }
|
1639 | 1646 |
|
1640 | 1647 |
|
| 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 | + |
1641 | 1780 | TEST t_tempdir(void){
|
1642 | 1781 | char *temp_dir = gettempdir();
|
1643 | 1782 |
|
@@ -1667,9 +1806,43 @@ TEST t_regex(void){
|
1667 | 1806 | if (match_idx >= 0) {
|
1668 | 1807 | printf("match at idx %i, %i chars long.\n", match_idx, match_length);
|
1669 | 1808 | }
|
| 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); |
1670 | 1827 |
|
1671 | 1828 | PASS();
|
1672 | 1829 | }
|
| 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 | + |
1673 | 1846 | SUITE(s_hidapi) {
|
1674 | 1847 | RUN_TEST(t_hidapi);
|
1675 | 1848 | }
|
@@ -1716,6 +1889,10 @@ GREATEST_MAIN_DEFS();
|
1716 | 1889 |
|
1717 | 1890 |
|
1718 | 1891 | 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); |
1719 | 1896 | GREATEST_MAIN_BEGIN();
|
1720 | 1897 | RUN_SUITE(s_json);
|
1721 | 1898 | RUN_SUITE(s_string);
|
@@ -1751,8 +1928,11 @@ int main(int argc, char **argv) {
|
1751 | 1928 | RUN_SUITE(s_tempdir);
|
1752 | 1929 | RUN_SUITE(s_hidapi);
|
1753 | 1930 | RUN_SUITE(s_libusb);
|
| 1931 | + RUN_SUITE(s_miniaudio); |
| 1932 | + RUN_SUITE(s_c89atomic); |
1754 | 1933 | GREATEST_MAIN_END();
|
1755 | 1934 | size_t used = do_dmt_summary();
|
| 1935 | + |
1756 | 1936 | dbg(used, %lu);
|
1757 | 1937 | assert(used == 0);
|
1758 | 1938 | } /* main */
|
0 commit comments