Пример #1
0
int main(int argc, char *argv[]) {
    char *filename;
    AIFF_Ref ref;
    uint64_t seconds, nSamples;
    int channels;
    double samplingRate;
    int bitsPerSample;
    int segmentSize;
    float buf[BUF_SIZE];
    int sampleCount;

    if (argc < 2) {
        fprintf(stderr, "Usage: %s <file.aiff>\n", argv[0]);
        exit(-1);
    }

    filename = argv[1];

    if((ref = AIFF_OpenFile(filename, F_RDONLY)) == NULL) {
    	fprintf(stderr, "Unable to open %s\n", filename);
        exit(-1);
    }

    if(AIFF_GetAudioFormat(ref, &nSamples, &channels,
    			&samplingRate, &bitsPerSample,
    			&segmentSize) < 1) {
    	fprintf(stderr, "Unable to get audio format for %s\n", filename);
        exit(-1);
    }

    seconds = nSamples / samplingRate ;
    /*
     * Print out the seconds in H:MM:SS format
     */
    printf("Length: %lu:%02lu:%02lu \n", (unsigned long) seconds/3600,
    	(unsigned long) (seconds/60)%60, (unsigned long) seconds%60 );

    std::cerr << filename << ":\n\t" <<
        "channels=" << channels <<
        ", sampleRate=" << samplingRate <<
        ", bytesPerSample=" << bitsPerSample / 8 <<
        ", numSamples=" << nSamples <<
        ", segmentSize=" << segmentSize <<
    std::endl;

    int c = 0;
    while ((sampleCount = AIFF_ReadSamplesFloat(ref, buf, BUF_SIZE)) > 0) {
        int i;
        for (i = 0; i < sampleCount; i++) {
            //printf("Sample %d: %f\n", c, buf[i]);
            c++;
        }
    }

    return 0;
}
Пример #2
0
//--------------------------------------------------------------
void ofApp::keyReleased(int key){
    
    if(key=='c') {
        cuttype = 1-cuttype;
        if(cuttype==0) themess = "cuttype is square";
        if(cuttype==1) themess = "cuttype is beats";
    }
    if(key=='s') {
        // draw picture
        themess = "sound to picture... saving";
        cout << "sound to picture... saving" << endl;
        const vector<float>& rawSamples = audio.getRawSamples();
        int channels = audio.getChannels();
        int n = rawSamples.size();
        
        if(cuttype==0) { // square picture
            int s = int(sqrt(n/channels));
            cout << "img size: " << s << " by " << s << endl;
            img.allocate(s, s, OF_IMAGE_COLOR);
            int iptr = 0; // which audio sample are we on?
            for(int i = 0;i<s;i++)
            {
                for(int j = 0;j<s;j++)
                {
                    float r = ofMap(rawSamples[iptr], -1., 1., 0., 65535.); // left side
                    float g = ofMap(rawSamples[iptr+1], -1., 1., 0., 65535.); // right side
                    cout << r << " " << g << endl;
                    img.setColor(j, i, ofShortColor(r, g, 0));
                    
                    iptr+=channels;
                }
            }
        }

        else if(cuttype==1) { // not square picture
            int nrows = 64;
            int subdivs = 4;
            int s = int((n/channels)/nrows);
            cout << "img size: " << s << " by " << nrows << endl;
            img.allocate(s, nrows, OF_IMAGE_COLOR);
            int iptr = 0; // which audio sample are we on?
            for(int i = 0;i<nrows;i++)
            {
                for(int j = 0;j<s;j++)
                {
                    float r = ofMap(rawSamples[iptr], -1., 1., 0., 65535.); // left side
                    float g = ofMap(rawSamples[iptr+1], -1., 1., 0., 65535.); // right side
                    float b = (j%(s/subdivs)==0)*65535.;
                    cout << r << " " << g << endl;
                    img.setColor(j, i, ofShortColor(r, g, b));
                    
                    iptr+=channels;
                }
            }
        }

        
        
        img.reloadTexture();
        img.saveImage("test.png");

    }
    if(key=='l') {
        themess = "picture to sound";
        // generate sound from picture
        cout << "picture to sound" << endl;
        ofShortImage foo;
        foo.loadImage("test.png");
        vector<int32_t> theSamps;
        cout << "size: " << foo.getWidth() << " by " << foo.getHeight() << endl;
        float s = foo.getWidth();
        float t = foo.getHeight();

        for(int i = 0;i<t;i++)
        {
            for(int j = 0;j<s;j++)
            {
                ofShortColor c = foo.getColor(j, i);
                float rs = ofMap(c.r, 0., 65535., -1., 1.);
                float gs = ofMap(c.g, 0., 65535., -1., 1.);
                int32_t r =int(rs*536870912.); // 2^29 (wtf?)
                int32_t g =int(gs*536870912.); // 2^29 (wtf?)
                cout << "sample: " << c.r << " " << c.g << endl;
                theSamps.push_back(r);
                theSamps.push_back(g);

            }
        }
 
        
        // try dumping the original sound
/*        const vector<float>& rawSamples = audio.getRawSamples();
        int channels = audio.getChannels();
        int n = rawSamples.size();
        for(int i = 0;i<n;i++)
        {
            int32_t s =int(rawSamples[i]*500000000.);
            cout << rawSamples[i] << " " <<s << endl;
            theSamps.push_back(s);
        }*/

        
        // insert file writing code to dump theSamps to disk here:
        
        AIFF_Ref ref ;
        
        string p = ofToDataPath("foo.aiff", true);
        cout << p << endl;
        
        ref = AIFF_OpenFile(p.c_str(), F_WRONLY) ;
        if( ref )
        {
            puts("File opened successfully.");
            AIFF_SetAudioFormat(ref, 2, 44100., 16);
            AIFF_StartWritingSamples(ref);
            int32_t* v = theSamps.data();
            AIFF_WriteSamples32Bit(ref, v, theSamps.size());
            AIFF_EndWritingSamples(ref);
            AIFF_CloseFile(ref);
            puts("Closed file.");
        }
    
    }

}