source: mainline/uspace/app/modplay/modplay.c@ 09ab0a9a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 09ab0a9a was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

  • Property mode set to 100644
File size: 3.8 KB
Line 
1/*
2 * Copyright (c) 2014 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup modplay
30 * @{
31 */
32/**
33 * @file
34 */
35
36#include <errno.h>
37#include <hound/client.h>
38#include <io/console.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <str_error.h>
42#include <trackmod.h>
43
44static bool quit = false;
45
46static void modplay_key_press(kbd_event_t *ev)
47{
48 if ((ev->mods & KM_ALT) == 0 &&
49 (ev->mods & KM_SHIFT) == 0 &&
50 (ev->mods & KM_CTRL) != 0) {
51 if (ev->key == KC_Q)
52 quit = true;
53 }
54}
55
56static void modplay_event(cons_event_t *event)
57{
58 switch (event->type) {
59 case CEV_KEY:
60 if (event->ev.key.type == KEY_PRESS) {
61 modplay_key_press(&event->ev.key);
62 }
63 break;
64 default:
65 break;
66 }
67}
68
69int main(int argc, char *argv[])
70{
71 trackmod_module_t *mod;
72 trackmod_modplay_t *modplay;
73 hound_context_t *hound;
74 console_ctrl_t *con;
75 cons_event_t event;
76 usec_t timeout;
77 pcm_format_t format;
78 void *buffer;
79 size_t buffer_size;
80 errno_t rc;
81
82 if (argc != 2) {
83 printf("syntax: modplay <filename.mod>\n");
84 return 1;
85 }
86
87 con = console_init(stdin, stdout);
88
89 rc = trackmod_module_load(argv[1], &mod);
90 if (rc != EOK) {
91 printf("Error loading %s.\n", argv[1]);
92 return 1;
93 }
94
95 format.channels = 1;
96 format.sampling_rate = 44100;
97#ifdef __LE__
98 format.sample_format = PCM_SAMPLE_SINT16_LE;
99#else
100 format.sample_format = PCM_SAMPLE_SINT16_BE;
101#endif
102 buffer_size = 64 * 1024;
103
104 buffer = malloc(buffer_size);
105 if (buffer == NULL) {
106 printf("Error allocating audio buffer.\n");
107 return 1;
108 }
109
110 hound = hound_context_create_playback(argv[1], format, buffer_size);
111 if (hound == NULL) {
112 printf("Error creating playback context.\n");
113 return 1;
114 }
115
116 rc = hound_context_connect_target(hound, HOUND_DEFAULT_TARGET);
117 if (rc != EOK) {
118 printf("Error connecting default audio target: %s.\n", str_error(rc));
119 return 1;
120 }
121
122 rc = trackmod_modplay_create(mod, format.sampling_rate, &modplay);
123 if (rc != EOK) {
124 printf("Error setting up playback.\n");
125 return 1;
126 }
127
128 printf("Playing '%s'. Press Ctrl+Q to quit.\n", argv[1]);
129
130 while (true) {
131 timeout = 0;
132 if (console_get_event_timeout(con, &event, &timeout))
133 modplay_event(&event);
134 if (quit)
135 break;
136
137 trackmod_modplay_get_samples(modplay, buffer, buffer_size / 4);
138
139 rc = hound_write_main_stream(hound, buffer, buffer_size / 4);
140 if (rc != EOK) {
141 printf("Error writing audio stream.\n");
142 break;
143 }
144 }
145
146 hound_context_destroy(hound);
147 trackmod_modplay_destroy(modplay);
148 trackmod_module_destroy(mod);
149
150 return 0;
151}
152
153/** @}
154 */
Note: See TracBrowser for help on using the repository browser.