00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <bse/gsldatautils.h>
00019 #include <stdio.h>
00020 #include <stdlib.h>
00021 #include <string.h>
00022
00023 static bool
00024 check_arg (uint argc,
00025 char *argv[],
00026 uint *nth,
00027 const char *opt,
00028 const char **opt_arg = NULL)
00029 {
00030 g_return_val_if_fail (opt != NULL, false);
00031 g_return_val_if_fail (*nth < argc, false);
00032
00033 const char *arg = argv[*nth];
00034 if (!arg)
00035 return false;
00036
00037 uint opt_len = strlen (opt);
00038 if (strcmp (arg, opt) == 0)
00039 {
00040 if (opt_arg && *nth + 1 < argc)
00041 {
00042 argv[(*nth)++] = NULL;
00043 *opt_arg = argv[*nth];
00044 argv[*nth] = NULL;
00045 return true;
00046 }
00047 else if (!opt_arg)
00048 {
00049 argv[*nth] = NULL;
00050 return true;
00051 }
00052
00053 }
00054 else if (strncmp (arg, opt, opt_len) == 0 && arg[opt_len] == '=')
00055 {
00056 if (opt_arg)
00057 {
00058 *opt_arg = arg + opt_len + 1;
00059 argv[*nth] = NULL;
00060 return true;
00061 }
00062
00063 }
00064 else
00065 return false;
00066
00067 Options::print_usage();
00068 exit (1);
00069 }
00070
00072
00073 class MiniResampler
00074 {
00075 GslDataHandle *m_dhandle;
00076 GslDataPeekBuffer m_peek_buffer;
00077 double m_speedup_factor;
00078 public:
00079 MiniResampler (GslDataHandle *dhandle, double speedup_factor);
00080
00081 int read (uint64 pos, size_t block_size, float *out);
00082 uint64 length();
00083 };
00085
00086 MiniResampler::MiniResampler (GslDataHandle *dhandle, double speedup_factor)
00087 {
00088 m_speedup_factor = speedup_factor;
00089 m_dhandle = dhandle;
00090 memset (&m_peek_buffer, 0, sizeof (m_peek_buffer));
00091 while (m_speedup_factor < 6)
00092 {
00093 m_dhandle = bse_data_handle_new_upsample2 (m_dhandle, 24);
00094 m_speedup_factor *= 2;
00095 BseErrorType error = gsl_data_handle_open (m_dhandle);
00096 if (error)
00097 {
00098 fprintf (stderr, "foo\n");
00099 exit (1);
00100 }
00101 }
00102 }
00103
00104 int
00105 MiniResampler::read (uint64 pos, size_t block_size, float *out)
00106 {
00107 uint64 n_values = gsl_data_handle_n_values (m_dhandle);
00108 for (size_t i = 0; i < block_size; i++)
00109 {
00110
00111 double dpos = (pos + i) * m_speedup_factor;
00112 uint64 left_pos = dpos;
00113 uint64 right_pos = left_pos + 1;
00114 if (right_pos >= n_values)
00115 return (i - 1);
00116 double fade = dpos - left_pos;
00117 out[i] = (1 - fade) * gsl_data_handle_peek_value (m_dhandle, left_pos, &m_peek_buffer)
00118 + fade * gsl_data_handle_peek_value (m_dhandle, right_pos, &m_peek_buffer);
00119 }
00120 return block_size;
00121 }
00122
00123 uint64
00124 MiniResampler::length()
00125 {
00126 return gsl_data_handle_n_values (m_dhandle) / m_speedup_factor;
00127 }
00128
00129