public void close() throws IOException {
        if(r != null) {
            r = null;
            ogg.close();
            ogg = null;
        }
        if(w != null) {
            w.bufferPacket(firstPacket.write(), true);
            w.bufferPacket(tags.write(), false);
            // TODO Write the others
            //w.bufferPacket(setup.write(), true);

            long lastGranule = 0;
            for(FlacAudioFrame fa : writtenAudio) {
                // Update the granule position as we go
                // TODO Track this
//              if(fa.getGranulePosition() >= 0 &&
//                 lastGranule != fa.getGranulePosition()) {
//                 w.flush();
//                 lastGranule = fa.getGranulePosition();
//                 w.setGranulePosition(lastGranule);
//              }

                // Write the data, flushing if needed
                w.bufferPacket(new OggPacket(fa.getData()));
                if(w.getSizePendingFlush() > 16384) {
                    w.flush();
                }
            }

            w.close();
            w = null;
            ogg.close();
            ogg = null;
        }
    }