1 /* GSequencer - Advanced GTK Sequencer
2  * Copyright (C) 2018 Joël Krähemann
3  *
4  * This file is part of GSequencer.
5  *
6  * GSequencer is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GSequencer is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GSequencer.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <glib.h>
21 #include <glib-object.h>
22 
23 #include <CUnit/CUnit.h>
24 #include <CUnit/Automated.h>
25 #include <CUnit/Basic.h>
26 
27 #include <ags/libags.h>
28 #include <ags/libags-audio.h>
29 
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <math.h>
34 
35 int ags_osc_action_controller_test_init_suite();
36 int ags_osc_action_controller_test_clean_suite();
37 
38 void ags_osc_action_controller_test_run_action();
39 
40 #define AGS_OSC_ACTION_CONTROLLER_TEST_CONFIG "[generic]\n" \
41   "autosave-thread=false\n"			       \
42   "simple-file=false\n"				       \
43   "disable-feature=experimental\n"		       \
44   "segmentation=4/4\n"				       \
45   "\n"						       \
46   "[thread]\n"					       \
47   "model=super-threaded\n"			       \
48   "super-threaded-scope=channel\n"		       \
49   "lock-global=ags-thread\n"			       \
50   "lock-parent=ags-recycling-thread\n"		       \
51   "\n"						       \
52   "[soundcard-0]\n"				       \
53   "backend=alsa\n"                                     \
54   "device=default\n"				       \
55   "samplerate=44100\n"				       \
56   "buffer-size=1024\n"				       \
57   "pcm-channels=2\n"				       \
58   "dsp-channels=2\n"				       \
59   "format=16\n"					       \
60   "\n"						       \
61   "[sequencer-0]\n"				       \
62   "backend=alsa\n"                                     \
63   "device=default\n"				       \
64   "\n"                                                 \
65   "[recall]\n"					       \
66   "auto-sense=true\n"				       \
67   "\n"
68 
69 AgsApplicationContext *application_context;
70 
71 AgsAudio *drum;
72 
73 GObject *default_soundcard;
74 
75 /* The suite initialization function.
76  * Opens the temporary file used by the tests.
77  * Returns zero on success, non-zero otherwise.
78  */
79 int
ags_osc_action_controller_test_init_suite()80 ags_osc_action_controller_test_init_suite()
81 {
82   AgsConfig *config;
83 
84   GList *start_audio;
85 
86   ags_priority_load_defaults(ags_priority_get_instance());
87 
88   config = ags_config_get_instance();
89   ags_config_load_from_data(config,
90 			    AGS_OSC_ACTION_CONTROLLER_TEST_CONFIG,
91 			    strlen(AGS_OSC_ACTION_CONTROLLER_TEST_CONFIG));
92 
93   application_context = ags_audio_application_context_new();
94   g_object_ref(application_context);
95 
96   ags_application_context_prepare(application_context);
97   ags_application_context_setup(application_context);
98 
99   default_soundcard = ags_sound_provider_get_default_soundcard(AGS_SOUND_PROVIDER(application_context));
100 
101   /* drum */
102   drum = ags_audio_new(default_soundcard);
103   g_object_ref(drum);
104 
105   g_object_set(drum,
106 	       "audio-name", "test-drum",
107 	       NULL);
108 
109   ags_audio_set_flags(drum,
110 		      (AGS_AUDIO_OUTPUT_HAS_RECYCLING |
111 		       AGS_AUDIO_INPUT_HAS_RECYCLING |
112 		       AGS_AUDIO_SYNC |
113 		       AGS_AUDIO_ASYNC));
114 
115   ags_audio_set_ability_flags(drum, (AGS_SOUND_ABILITY_PLAYBACK |
116 				     AGS_SOUND_ABILITY_SEQUENCER |
117 				     AGS_SOUND_ABILITY_NOTATION));
118 
119   ags_audio_set_audio_channels(drum,
120 			       2, 0);
121 
122   ags_audio_set_pads(drum,
123 		     AGS_TYPE_OUTPUT,
124 		     1, 0);
125   ags_audio_set_pads(drum,
126 		     AGS_TYPE_INPUT,
127 		     8, 0);
128 
129   start_audio = ags_sound_provider_get_audio(AGS_SOUND_PROVIDER(application_context));
130   ags_sound_provider_set_audio(AGS_SOUND_PROVIDER(application_context),
131 			       g_list_prepend(start_audio,
132 					      drum));
133 
134   return(0);
135 }
136 
137 /* The suite cleanup function.
138  * Closes the temporary file used by the tests.
139  * Returns zero on success, non-zero otherwise.
140  */
141 int
ags_osc_action_controller_test_clean_suite()142 ags_osc_action_controller_test_clean_suite()
143 {
144   return(0);
145 }
146 
147 void
ags_osc_action_controller_test_run_action()148 ags_osc_action_controller_test_run_action()
149 {
150   AgsOscConnection *osc_connection;
151 
152   AgsOscActionController *osc_action_controller;
153 
154   GList *osc_response;
155 
156   static const unsigned char *start_soundcard_message = "/action\x00,ss\x00/AgsSoundProvider/AgsSoundcard\x00\x00start\x00\x00\x00";
157   static const unsigned char *start_sequencer_message = "/action\x00,ss\x00/AgsSoundProvider/AgsSequencer\x00\x00start\x00\x00\x00";
158   static const unsigned char *start_audio_message =     "/action\x00,ss\x00/AgsSoundProvider/AgsAudio[0]\x00\x00\x00start\x00\x00\x00";
159   static const unsigned char *stop_soundcard_message = "/action\x00,ss\x00/AgsSoundProvider/AgsSoundcard\x00\x00stop\x00\x00\x00\x00";
160   static const unsigned char *stop_sequencer_message = "/action\x00,ss\x00/AgsSoundProvider/AgsSequencer\x00\x00stop\x00\x00\x00\x00";
161   static const unsigned char *stop_audio_message =     "/action\x00,ss\x00/AgsSoundProvider/AgsAudio[0]\x00\x00\x00stop\x00\x00\x00\x00";
162 
163   static const guint start_soundcard_message_size = 52;
164   static const guint start_sequencer_message_size = 52;
165   static const guint start_audio_message_size = 52;
166   static const guint stop_soundcard_message_size = 52;
167   static const guint stop_sequencer_message_size = 52;
168   static const guint stop_audio_message_size = 52;
169 
170   osc_connection = ags_osc_connection_new(NULL);
171 
172   osc_action_controller = ags_osc_action_controller_new();
173 
174   /* soundcard */
175   osc_response = ags_osc_action_controller_run_action(osc_action_controller,
176 						      osc_connection,
177 						      start_soundcard_message, start_soundcard_message_size);
178 
179   CU_ASSERT(osc_response != NULL);
180 
181   /* sequencer */
182   osc_response = ags_osc_action_controller_run_action(osc_action_controller,
183 						      osc_connection,
184 						      start_sequencer_message, start_sequencer_message_size);
185 
186   CU_ASSERT(osc_response != NULL);
187 
188   /* audio */
189   osc_response = ags_osc_action_controller_run_action(osc_action_controller,
190 						      osc_connection,
191 						      start_audio_message, start_audio_message_size);
192 
193   CU_ASSERT(osc_response != NULL);
194 
195   /* wait a moment */
196   sleep(10);
197 
198   /* soundcard */
199   osc_response = ags_osc_action_controller_run_action(osc_action_controller,
200 						      osc_connection,
201 						      stop_soundcard_message, stop_soundcard_message_size);
202 
203   CU_ASSERT(osc_response != NULL);
204 
205   /* sequencer */
206   osc_response = ags_osc_action_controller_run_action(osc_action_controller,
207 						      osc_connection,
208 						      stop_sequencer_message, stop_sequencer_message_size);
209 
210   CU_ASSERT(osc_response != NULL);
211 
212   /* audio */
213   osc_response = ags_osc_action_controller_run_action(osc_action_controller,
214 						      osc_connection,
215 						      stop_audio_message, stop_audio_message_size);
216 
217   CU_ASSERT(osc_response != NULL);
218 }
219 
220 int
main(int argc,char ** argv)221 main(int argc, char **argv)
222 {
223   CU_pSuite pSuite = NULL;
224 
225   putenv("LC_ALL=C");
226   putenv("LANG=C");
227 
228   putenv("LADSPA_PATH=\"\"");
229   putenv("DSSI_PATH=\"\"");
230   putenv("LV2_PATH=\"\"");
231 
232   /* initialize the CUnit test registry */
233   if(CUE_SUCCESS != CU_initialize_registry()){
234     return CU_get_error();
235   }
236 
237   /* add a suite to the registry */
238   pSuite = CU_add_suite("AgsOscActionControllerTest", ags_osc_action_controller_test_init_suite, ags_osc_action_controller_test_clean_suite);
239 
240   if(pSuite == NULL){
241     CU_cleanup_registry();
242 
243     return CU_get_error();
244   }
245 
246   /* add the tests to the suite */
247   if((CU_add_test(pSuite, "test of AgsOscActionController run action", ags_osc_action_controller_test_run_action) == NULL)){
248     CU_cleanup_registry();
249 
250     return CU_get_error();
251   }
252 
253   /* Run all tests using the CUnit Basic interface */
254   CU_basic_set_mode(CU_BRM_VERBOSE);
255   CU_basic_run_tests();
256 
257   CU_cleanup_registry();
258 
259   return(CU_get_error());
260 }
261