aboutsummaryrefslogtreecommitdiff
path: root/Plugins/sonogram.cpp
blob: 68c86fe9cdc3483ae555f6582320e4298ed29aec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
// sonogram.cpp
//
// use with fs.sonogram
//
// see example-sonogram.pd
//

#include <stdlib.h>
#include <stdio.h>
#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include "plugin.h"
#include "pixels.h"

#define SONOSIZE 128
#define SONOMAXVAL 500
#define sonotype float

INFO("helper for fs.sonogram")

int callcount=-1;

void perform_effect(_frame f, _args a)
{
	if(!a.s) return;

	if(++callcount>=f.width) callcount=0;

	FILE *inf;
	if((inf = fopen(a.s, "rb"))==NULL)
	{
		printf("sonogram: error opening %s\n", a.s);
		return;
	}

	char buf[80];
	sonotype sono[SONOSIZE];
	int i=0;

	while(!feof(inf))
	{
		if(!fgets(buf, 80, inf))
		{
			printf("sonogram: read error.\n");
			return;
		}
		sono[i] = atof(buf);
		if(++i>=SONOSIZE) break;
	}
	fclose(inf);

	int y;

	for(i=0; i<SONOSIZE; i++)
	{
		// scale values 0-500 to 0-255
		if(sono[i]>SONOMAXVAL) sono[i]=SONOMAXVAL;
		sono[i] = (sonotype)((sono[i] / (float)SONOMAXVAL)*255);
		// white = no sound
		sono[i] = abs(sono[i]-255);

		y = (int)((i / (float)SONOSIZE) * (float)f.height);
		// bottom = bass
		y = abs((f.height-1) - y);

		switch(f.pixelformat)
		{
			case 16:
				scanline16(f, y)[callcount] =
				 rgbtocolor16(sono[i], sono[i], sono[i]);
				break;
			case 24:
				scanline24(f, y)[callcount] =
				 rgbtocolor24(sono[i], sono[i], sono[i]);
				break;
			case 32:
				scanline32(f, y)[callcount] =
				 rgbtocolor32(sono[i], sono[i], sono[i]);
				break;
		}
	}
}