Vuo  2.3.2
VuoAudioSamples.c
Go to the documentation of this file.
1 
10 #include <stdlib.h>
11 #include "type.h"
12 
14 #ifdef VUO_COMPILER
16  "title" : "Audio Samples",
17  "description" : "A set of audio amplitudes for a single channel.",
18  "keywords" : [ ],
19  "version" : "1.0.0",
20  "dependencies" : [
21  "VuoInteger",
22  "VuoReal",
23  "VuoText"
24  ]
25  });
26 #endif
28 
31 
43 {
44  VuoAudioSamples value = {0, NULL, 0};
45  json_object *o = NULL;
46 
47  if (json_object_object_get_ex(js, "samples", &o))
48  {
49  int sampleCount = json_object_array_length(o);
50  if (sampleCount)
51  {
52  value = VuoAudioSamples_alloc(sampleCount);
53 
54  for (VuoInteger i = 0; i < sampleCount; ++i)
55  value.samples[i] = json_object_get_double(json_object_array_get_idx(o, i));
56  }
57  else
58  {
59  value.sampleCount = 0;
60  value.samples = NULL;
61  }
62  }
63 
64  if (json_object_object_get_ex(js, "samplesPerSecond", &o))
66 
67  return value;
68 }
69 
74 {
75  json_object *js = json_object_new_object();
76 
77  json_object *samplesObject = json_object_new_array();
78  for (VuoInteger i=0; i<value.sampleCount; ++i)
79  json_object_array_add(samplesObject, VuoReal_getJson(value.samples[i]));
80  json_object_object_add(js, "samples", samplesObject);
81 
82  json_object *samplesPerSecondObject = VuoReal_getJson(value.samplesPerSecond);
83  json_object_object_add(js, "samplesPerSecond", samplesPerSecondObject);
84 
85  return js;
86 }
87 
92 {
93  return VuoText_format("%lld samples @ %g kHz", value.sampleCount, value.samplesPerSecond/1000);
94 }
95 
100 {
101  VuoAudioSamples value;
102 
103  value.sampleCount = sampleCount;
104 
105  value.samples = (VuoReal *)malloc(sizeof(VuoReal)*value.sampleCount);
106  VuoRegister(value.samples, free);
107 
108  return value;
109 }
110 
117 {
118  if (samples.sampleCount == 0)
119  return true;
120 
121  for (VuoInteger i = 0; i < samples.sampleCount; ++i)
122  if (fabs(samples.samples[i]) >= 0.0001)
123  return false;
124 
125  return true;
126 }
127 
133 {
134  return (samples.sampleCount > 0);
135 }