Professional Documents
Culture Documents
4. Interactive audio
See in-depth details in my book Mastering openFrameworks Books examples are free, see masteringof.wordpress.com
http://blog.modernmechanix.com/mags/qf/c/PopularScience/9-1950/med_sound.jpg
Amplitude
Time
http://upload.wikimedia.org/wikipedia/commons/thumb/9/9a/Digital.signal.svg/567px-Digital.signal.svg.png
Sampling frequency
8,000 Hz - telephone, enough for speech. 11,025 Hz - games, samples for electronic music. 22 050 Hz - the same as 11,025 Hz.
Capacity
Length - the number of bits used to represent the signal samples in the quantization (in our case - the quantization of the amplitude).
2. You can change the images based on the values of neighboring pixels (for example, the operation of smoothing).
For audio in PCM format for both possibilities not applicable
3. mi 2 nd octave 659.26 Hz
1.
+ 3.
The last two sound sound the same. And their function amplitude - much different. Thus, chelovecheckoe ear perceives the sound spectrum, ie the composition of its frequency, not amplitude representation of sound.
6. S & S - Sample & Synthesis - Sampling, analysis and subsequent synthesis today one of the best technologies play "live" instruments.
Sampling
Recording: "Live Sound" - a microphone - ADC - PCM-format. Playback: PCM-format - DAC - speaker.
Additional options: you can change the playback speed, then increase the tone and speed of the sample. Modern algorithms also enable you to change the tone of the sample without changing its speed, and vice versa.
http://josephdbferguson.com/uploads/akai_mpc1000.jpg
(Subtractive) Synthesis
In precomputer time: a few simple waves (rectangular, sinusoidal, triangular) processed a set of filters (bass, treble, cut the desired frequency). The resultant was going to the speakers. Now: done digitally. There are difficulties - should carefully consider the problems associated with the digital representation of sound ("aliasing").
Synthesizer Minimoog
http://www.jarrography.free.fr/synths/images/moog_minimoog.jpg
// Declare variables ofSoundPlayer sample;// Sample player ofPoint p; // point and the radius - to draw a circle float rad; void testApp:: setup () { sample.loadSound ("sound.wav"); // Load sample from the folder bin / data sample.setVolume (0.5f);// Volume [0, 1] sample.setMultiPlay (true);// Allow you to run multiple samples ofSetFrameRate (60) // speed drawing frame ofSetBackgroundAuto (false); // turn off background erase ofBackground (255,255,255); }
Additive synthesis
Additive synthesis based on the construction of sound by summing a set of harmonics (ie, sine waves of different frequencies) with variable volume.
Any sound can be represented with arbitrary accuracy as the sum of a large number of harmonics with varying volume. But in practice, work with a large number of harmonics requires large computational resources. Although, at present, there are several hardware and software additive synthesizers.
Harmonics are played with looped samples, which simply changes the volume.
Synth Code 1 / 4
// Declare variables // Video-grabber for "capture" the video frames ofVideoGrabber grabber; int w;// Width of the frame int h;// Height of the frame const int n = 20;// Number of harmonics ofSoundPlayer sample [n];// Samples of harmonics float volume [n]; // Volume of harmonics int N [n];// Number of pixels in the play harmonica ofSoundPlayer sampleLoop; // Sample a drum loop
Synth Code 2 / 4
// Initialize void testApp:: setup () { w = 320; h = 240; grabber.initGrabber (w, h); // Connect the camera // Grab samples harmonics for (int i = 0; i <n; i + +) { int freq = (i +1) * 100; sample [i]. loadSound (ofToString (freq) + ". wav"); // Files are called 100.wav, ... sample [i]. setVolume (0.0);// Volume sample [i]. setLoop (true);// Looping Sound sample [i]. play ();// Start sound }
Synth Code 3 / 4
// Update the state void testApp:: update () { grabber.grabFrame (); // grab a frame if (grabber.isFrameNew ()) {// if you come to a new frame for (int i = 0; i <n; i + +) {volume [i] = 0; N [i] = 0;} // Reset the harmonic unsigned char * input = grabber.getPixels (); // pixels of the input image for (int y = 0; y <h; y + +) { for (int x = 0; x <w; x + +) { // Input pixel (x, y): int r = input [3 * (x + w * y) + 0]; int g = input [3 * (x + w * y) + 1]; int b = input [3 * (x + w * y) + 2]; int result = (r + g + b> 400)? 0: 1;// Threshold int i = (x * n / w);// In which to write the result of harmonic volume [i] + = result; N [i] + +; }} // Set the new volume of harmonics for (int i = 0; i <n; i + +) { if (N [i]> 0) {volume [i] / = N [i];} // Normalize the volume [0, 1] sample [i]. setVolume (volume [i] / n); // Volume. // Divide by n, otherwise it will be distortion of the output sound }} OfSoundUpdate ();// Update the status of the sound system }
Synth Code 4 / 4
// Draw void testApp:: draw () { ofBackground (255,255,255); // Set the background color float w = ofGetWidth ();// Screen height and width float h = ofGetHeight (); ofSetColor (255, 255, 255); // Else draw a picture frame is incorrect grabber.draw (0, 0, w, h);// Output frame
// Draw the harmonics ofEnableAlphaBlending (); // Enable transparency ofSetColor (0, 0, 255, 80);// Blue color with opacity of 80 for (int i = 0; i <n; i + +) { float harmH = volume [i] * h;// Height of the bar harmonics i ofRect (i * w / n, h - harmH, w / n, harmH); } ofDisableAlphaBlending ();// Disable transparency }
http://www.youtube.com/watch?v=y70Oxk1RAOM
Introduction
Sound synthesis on openFrameworks performed at the lowest level, "byte".
Therefore it is suitable esperimentalnyh projects with sound. In complex projects, it is more convenient to use a special type of library SndObj (See enlargement oxfSndObj) or some other program like PureData or Max / MSP, which binds openFrameworks protocol OSC.
Program Structure
For sound synthesis conventional structure of the program improves audioRequested (). It is called the sound driver, when you need to fill in another piece of sound buffer sound.
Program Structure
in testApp.h, class testApp add: void audioRequested (float * input, int bufferSize, int nChannels); in the setup () to add: ofSoundStreamSetup (2,0, this, 22050, 256, 4); / 2 output channels, // 0 input channels, // 22050 - sampling rate, samples per second // 256 - buffer size // 4 - how to use buffers. Affects the delay. // Buffer size and number of buffers - set the balance between delay and the resulting sound, "Glitter," which occurs when the computer is not fast enough.
Program Structure
in testApp.cpp add: void testApp::audioRequested (Float * output, // output buffer int bufferSize, // buffer size int nChannels // number of channels ) { // Example of sound "white noise" to two channels for (int i = 0; i <bufferSize; i + +) { output [i * nChannels] = ofRandomf (); // [-1,1] output [i * nChannels + 1] = ofRandomf (); } }
Example
See an example audioOutputExample in OpenFrameworks.
Mouse moves 1. up and down - changing the tone of the sound. 2. left and right - changing panorama. Mouse click - generated noise.
http://www.youtube.com/watch?v=Pz6PO4H1LT0
Homework
Using the example audioOutputExample, add to the example of the swinging pendulum of sound generation. Namely: Let the position of the pendulum in the Y sets the pitch, and the position of the pendulum on X - panning.