Statistics
| Branch: | Revision:

ddr4s / fw / wiringPi / examples / Gertboard / vumeter.c @ 32:cadb9025f1e0

History | View | Annotate | Download (3.23 KB)

1
/*
2
 * vumeter.c:
3
 *        Simple VU meter
4
 *
5
 * Heres the theory:
6
 *        We will sample at 4000 samples/sec and put the data into a
7
 *        low-pass filter with a depth of 1000 samples. This will give
8
 *        us 1/4 a second of lag on the signal, but I think it might
9
 *        produce a more pleasing output.
10
 *
11
 *        The input of the microphone should be at mid-pont with no
12
 *        sound input, but we might have to sample that too, to get
13
 *        our reference zero...
14
 *
15
 *        Copyright (c) 2013 Gordon Henderson
16
 ***********************************************************************
17
 */
18

    
19
#include <stdio.h>
20
#include <stdlib.h>
21
#include <sys/time.h>
22

    
23
#include <wiringPi.h>
24
#include <gertboard.h>
25

    
26
#ifndef        TRUE
27
#define        TRUE        (1==1)
28
#define        FALSE        (!TRUE)
29
#endif
30

    
31
#define        B_SIZE        1000
32
#define        S_SIZE         128
33

    
34
static int buffer [B_SIZE] ;
35
static int bPtr = 0 ;
36

    
37
/*
38
 * ledPercent:
39
 *        Output the given value as a percentage on the LEDs
40
 *********************************************************************************
41
 */
42

    
43
static void ledPercent (int percent)
44
{
45
  unsigned int output = 0 ;
46

    
47
  if (percent > 11) output |= 0x01 ;
48
  if (percent > 22) output |= 0x02 ;
49
  if (percent > 33) output |= 0x04 ;
50
  if (percent > 44) output |= 0x08 ;
51
  if (percent > 55) output |= 0x10 ;
52
  if (percent > 66) output |= 0x20 ;
53
  if (percent > 77) output |= 0x40 ;
54
  if (percent > 88) output |= 0x80 ;
55

    
56
  digitalWriteByte (output) ;
57
}
58

    
59
static unsigned int tPeriod, tNextSampleTime ;
60

    
61
/*
62
 * sample:
63
 *        Get a sample from the Gertboard. If not enough time has elapsed
64
 *        since the last sample, then wait...
65
 *********************************************************************************
66
 */
67

    
68
static void sample (void)
69
{
70
  unsigned int tFuture ;
71

    
72
// Calculate the future sample time
73

    
74
  tFuture = tPeriod + tNextSampleTime ;
75

    
76
// Wait until the next sample time
77

    
78
  while (micros () < tNextSampleTime)
79
    ;
80
  
81
  buffer [bPtr] = gertboardAnalogRead (0) ;
82

    
83
  tNextSampleTime = tFuture ;
84
}
85

    
86

    
87
int main ()
88
{
89
  int quietLevel, min, max ;
90
  int i, sum ;
91
  unsigned int tStart, tEnd ;
92

    
93
  printf ("\n") ;
94
  printf ("Gertboard demo: VU Meter\n") ;
95
  printf ("========================\n") ;
96

    
97
  wiringPiSetup     () ;
98
  gertboardSPISetup () ;
99

    
100
  ledPercent (0) ;
101
  for (i = 0 ; i < 8 ; ++i)
102
    pinMode (i, OUTPUT) ;
103

    
104
  for (bPtr = 0 ; bPtr < B_SIZE ; ++bPtr)
105
    buffer [bPtr] = 99 ;
106

    
107
  tPeriod = 1000000 / 1000 ;
108

    
109
  printf ("Shhhh.... ") ; fflush (stdout) ;
110
  delay (1000) ;
111
  printf ("Sampling quiet... ") ; fflush (stdout) ;
112

    
113
  tStart = micros () ;
114

    
115
  tNextSampleTime = micros () ;
116
  for (bPtr = 0 ; bPtr < B_SIZE ; ++bPtr)
117
    sample () ;
118

    
119
  tEnd = micros () ;
120

    
121
  quietLevel = 0 ;
122
  max =    0 ;
123
  min = 1024 ;
124
  for (i = 0 ; i < B_SIZE ; ++i)
125
  {
126
    quietLevel += buffer [i] ;
127
    if (buffer [i] > max) max = buffer [i] ;
128
    if (buffer [i] < min) min = buffer [i] ;
129
  }
130
  quietLevel /= B_SIZE ;
131

    
132
  printf ("Done. Quiet level is: %d [%d:%d] [%d:%d]\n", quietLevel, min, max, quietLevel - min, max - quietLevel) ;
133

    
134
  printf ("Time taken for %d reads: %duS\n", B_SIZE, tEnd - tStart) ;
135

    
136
  for (bPtr = 0 ;;)
137
  {
138
    sample () ;
139
    sum = 0 ;
140
    for (i = 0 ; i < S_SIZE ; ++i)
141
      sum += buffer [i] ;
142
    sum /= S_SIZE ;
143
    sum = abs (quietLevel - sum) ;
144
    sum = (sum * 1000) / quietLevel ;
145
    ledPercent (sum) ;
146
    if (++bPtr > S_SIZE)
147
      bPtr = 0 ;
148
  }
149

    
150

    
151
  return 0 ;
152
}