-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevel27.cs
More file actions
96 lines (91 loc) · 3.52 KB
/
level27.cs
File metadata and controls
96 lines (91 loc) · 3.52 KB
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
86
87
88
89
90
91
92
93
94
95
96
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System;
/*
* * * * * * * * * * * * * * * * * * *
* Agustín Rojas - ITChallenge 2017 *
* *
* Nivel 27 - Uruguay *
* * * * * * * * * * * * * * * * * * *
*
* POR SOLUCIONAR: DURACION DEL WAV MAYOR AL DE LA CANCION
*/
namespace ITChallenge
{
class level27
{
static void Main(string[] args)
{
//Abrimos la imagen
Bitmap Imagen = new Bitmap("level27.png");
//Conseguimos su tamaño
int alto = Imagen.Height;
int ancho = Imagen.Width;
//Creamos una lista para almacenar los valores de la onda de sonido
List<double> lista = new List<double>();
//for anidados para trabajar con cada pixel de la imagen ordenadamente
for (int y = 0; y < alto; y++)
{
for (int x = 0; x < ancho; x++)
{
//conseguimos el color del pixel
Color pixel = Imagen.GetPixel(x, y);
//Agregamos a la onda los valores RGB y Alfa
lista.Add(pixel.R);
lista.Add(pixel.G);
lista.Add(pixel.B);
lista.Add(pixel.A);
}
}
//Pasamos la lista a array para poder exportar en wav
double[] onda = lista.ToArray();
//exportamos pasando la onda, el tamaño y la frecuencia (en la mayoria de los casos es 441000, en este tambien
SaveIntoStream(onda, onda.Length, 44100);
}
//INICIO CODIGO AJENO
//Fuente: https://sysdot.wordpress.com/2011/03/24/making-a-simple-audio-synthesizer-in-c/
public static void SaveIntoStream(double[] sampleData, long sampleCount, int samplesPerSecond)
{
// Export
FileStream stream = File.Create("test.wav");
System.IO.BinaryWriter writer = new System.IO.BinaryWriter(stream);
int RIFF = 0x46464952;
int WAVE = 0x45564157;
int formatChunkSize = 16;
int headerSize = 8;
int format = 0x20746D66;
short formatType = 1;
short tracks = 1;
short bitsPerSample = 16;
short frameSize = (short)(tracks * ((bitsPerSample + 7) / 8));
int bytesPerSecond = samplesPerSecond * frameSize;
int waveSize = 4;
int data = 0x61746164;
int samples = (int)sampleCount;
int dataChunkSize = samples * frameSize;
int fileSize = waveSize + headerSize + formatChunkSize + headerSize + dataChunkSize;
writer.Write(RIFF);
writer.Write(fileSize);
writer.Write(WAVE);
writer.Write(format);
writer.Write(formatChunkSize);
writer.Write(formatType);
writer.Write(tracks);
writer.Write(samplesPerSecond);
writer.Write(bytesPerSecond);
writer.Write(frameSize);
writer.Write(bitsPerSample);
writer.Write(data);
writer.Write(dataChunkSize);
//FIN CODIGO AJENO
//grabamos en el archivo cada valor de la onda
foreach (double d in sampleData)
{
writer.Write((byte)d);
}
Console.WriteLine("TERMINADO");
Console.Read();
}
}
}