summaryrefslogtreecommitdiffstats
path: root/src/sequencer/MmappedSegment.h
blob: 0beceaded0d6639bd7bf368f7150e417bb9f6a86 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185

/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */

/*
    Rosegarden
    A MIDI and audio sequencer and musical notation editor.

    This program is Copyright 2000-2008
        Guillaume Laurent   <glaurent@telegraph-road.org>,
        Chris Cannam        <cannam@all-day-breakfast.com>,
        Richard Bown        <richard.bown@ferventsoftware.com>

    The moral rights of Guillaume Laurent, Chris Cannam, and Richard
    Bown to claim authorship of this work have been asserted.

    Other copyrights also apply to some parts of this work.  Please
    see the AUTHORS file and individual file headers for details.

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/

#ifndef _MMAPPED_SEGMENT_H_
#define _MMAPPED_SEGMENT_H_

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <errno.h>

#include <iostream>

#include <klocale.h>
#include <kstandarddirs.h>

#include <dcopclient.h>
#include <tqdatetime.h>
#include <tqstring.h>
#include <tqdir.h>
#include <tqbuffer.h>

#include "ControlBlockMmapper.h"
#include "sound/MappedInstrument.h"

namespace Rosegarden {

class MappedComposition;

// Seems not to be properly defined under some gcc 2.95 setups
#ifndef MREMAP_MAYMOVE
# define MREMAP_MAYMOVE        1
#endif

/**
 * An mmap()ed segment
 */
class MmappedSegment
{
public:
    MmappedSegment(const TQString filename);
    ~MmappedSegment();

    bool remap(size_t newSize);
    TQString getFileName() const { return m_filename; }
    bool isMetronome();
    MappedEvent* getBuffer() { return m_mmappedEventBuffer; }
    size_t getSize() const { return m_mmappedSize; }
    size_t getNbMappedEvents() const;

    class iterator 
    {
    public:
        iterator(MmappedSegment* s);
        iterator& operator=(const iterator&);
        bool operator==(const iterator&);
        bool operator!=(const iterator& it) { return !operator==(it); }

        bool atEnd() const;

        /// go back to beginning of stream
        void reset();

        iterator& operator++();
        iterator  operator++(int);
        iterator& operator+=(int);
        iterator& operator-=(int);

        const MappedEvent &operator*();
        const MappedEvent* peek() const { return m_currentEvent; }

        MmappedSegment* getSegment() { return m_s; }
        const MmappedSegment* getSegment() const { return m_s; }

    private:
         iterator();

    protected:
        //--------------- Data members ---------------------------------

        MmappedSegment* m_s;
        MappedEvent* m_currentEvent;
    };

protected:
    void map();
    void unmap();

    //--------------- Data members ---------------------------------
    int m_fd;
    size_t m_mmappedSize;
//    unsigned int m_nbMappedEvents;
    void *m_mmappedRegion;
    MappedEvent* m_mmappedEventBuffer;
    TQString m_filename;
};

class MmappedSegmentsMetaIterator
{
public:

    typedef std::map<TQString, MmappedSegment*> mmappedsegments;

    MmappedSegmentsMetaIterator(mmappedsegments&,
                                ControlBlockMmapper*);
    ~MmappedSegmentsMetaIterator();

    /// reset all iterators to beginning
    void reset();
    bool jumpToTime(const RealTime&);

    /**
     * Fill mapped composition with events from current point until
     * specified time @return true if there are non-metronome events
     * remaining, false if end of composition was reached
     */
    bool fillCompositionWithEventsUntil(bool firstFetch,
                                        MappedComposition*,
                                        const RealTime& start,
                                        const RealTime& end);

    void resetIteratorForSegment(const TQString& filename);

    void addSegment(MmappedSegment*);
    void deleteSegment(MmappedSegment*);

    void getAudioEvents(std::vector<MappedEvent> &);

    // Manipulate a vector of currently mapped audio segments so that we
    // can cross check them against PlayableAudioFiles (and stop if
    // necessary).  This will account for muting/soloing too I should
    // hope.
    //
    //!!! to be obsoleted, hopefully
    std::vector<MappedEvent>& getPlayingAudioFiles
    (const RealTime &songPosition);

protected:
    bool acceptEvent(MappedEvent*, bool evtIsFromMetronome);

    /// Delete all iterators
    void clear();
    bool moveIteratorToTime(MmappedSegment::iterator&,
                            const RealTime&);

    //--------------- Data members ---------------------------------

    ControlBlockMmapper* m_controlBlockMmapper;

    RealTime m_currentTime;
    mmappedsegments& m_segments;

    typedef std::vector<MmappedSegment::iterator*> segmentiterators;
    segmentiterators                               m_iterators;

    std::vector<MappedEvent>                       m_playingAudioSegments;
};

}

#endif // _MMAPPED_SEGMENT_H_