2 * Any copyright is dedicated to the Public Domain.
3 * http://creativecommons.org/publicdomain/zero/1.0/
6 #define _POSIX_C_SOURCE 199309L
8 #include "cin_driver.h"
9 #include "cin_loader.h"
10 #include "cin_sound.h"
17 /* Get a sleep function. */
19 #define WIN32_LEAN_AND_MEAN
27 #define M_PI 3.14159265f
30 int main(int argc, char **argv){
31 struct Cin_Driver *const driver = malloc(Cin_StructDriverSize());
33 puts("Could not allocate driver.");
40 assert(Cin_StructDriverSize() > 0);
41 assert(Cin_StructLoaderSize() > 0);
42 assert(Cin_StructSoundSize() > 0);
44 if(driver == NULL || Cin_CreateDriver(driver) == Cin_eDriverSuccess){
45 struct Cin_Loader *const loader = malloc(Cin_StructLoaderSize());
46 struct Cin_Sound *const sound = malloc(Cin_StructSoundSize());
48 /* TODO: Make these configurable */
49 unsigned num_channels = 2,
54 /* Times two for the size of int16_t */
55 const unsigned data_size = sample_rate * num_channels << 1;
56 short *const data = malloc(data_size);
58 if(!(loader != NULL && sound != NULL && data != NULL)){
60 puts("Could not allocate loader.");
62 puts("Could not allocate sound.");
64 puts("Could not allocate buffer for generating audio.");
68 Cin_DestroyDriver(driver);
74 const enum Cin_LoaderError err =
75 Cin_CreateLoader(loader,
80 if(err != Cin_eLoaderSuccess){
84 Cin_DestroyDriver(driver);
86 puts("Could not create loader.");
92 const float multiplier = M_PI * 2 * (float)sin_frequency;
94 /* Create one second of 44100 Hz audio */
95 /* We want to cycle sequence 440 times a second. */
96 for(i = 0; i < sample_rate; i++){
97 const float sequence =
98 multiplier * (float)i / (float)sample_rate;
99 const float sample = sinf(sequence) * (float)(0xFFFF >> 1);
100 const unsigned at = i * num_channels;
101 data[at] = data[at + 1] = (short)(sample * 0.1);
104 /* Put the second once for each second of data we want to play. */
105 for(i = 0; i < num_seconds; i++){
106 Cin_LoaderPut(loader, data, data_size);
110 /* Create a new sound from this loader. */
112 const enum Cin_LoaderError err =
113 Cin_LoaderFinalize(loader, sound);
118 if(err != Cin_eLoaderSuccess){
120 Cin_DestroyDriver(driver);
122 puts("Could not create sound.");
128 Cin_SoundPlay(sound);
131 Sleep((1000 * num_seconds) + 1);
135 t.tv_sec = num_seconds;
141 Cin_SoundStop(sound);
142 Cin_DestroySound(sound);
146 puts("Could not create driver.");
150 Cin_DestroyDriver(driver);