~ubuntu-branches/ubuntu/lucid/lastfm/lucid

« back to all changes in this revision

Viewing changes to src/transcode/mad/RingBuffer.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Pedro Fragoso
  • Date: 2007-12-31 09:49:54 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20071231094954-ix1amvcsj9pk61ya
Tags: 1:1.4.1.57486.dfsg-1ubuntu1
* Merge from Debian unstable (LP: #180254), remaining changes:
  - debian/rules;
    - Added dh_icons
  - Modify Maintainer value to match Debian-Maintainer-Field Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2005 - 2007 by                                          *
 
3
 *      John Stamp                                                         *
 
4
 *      Max Howell, Last.fm Ltd <max@last.fm>                              *
 
5
 *                                                                         *
 
6
 *   This program is free software; you can redistribute it and/or modify  *
 
7
 *   it under the terms of the GNU General Public License as published by  *
 
8
 *   the Free Software Foundation; either version 2 of the License, or     *
 
9
 *   (at your option) any later version.                                   *
 
10
 *                                                                         *
 
11
 *   This program is distributed in the hope that it will be useful,       *
 
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
14
 *   GNU General Public License for more details.                          *
 
15
 *                                                                         *
 
16
 *   You should have received a copy of the GNU General Public License     *
 
17
 *   along with this program; if not, write to the                         *
 
18
 *   Free Software Foundation, Inc.,                                       *
 
19
 *   51 Franklin Steet, Fifth Floor, Boston, MA  02110-1301, USA.          *
 
20
 ***************************************************************************/
 
21
 
 
22
#include "RingBuffer.h"
 
23
 
 
24
#include <logger.h>
 
25
#include <QByteArray>
 
26
#include <stdlib.h>
 
27
#include <QtGlobal>
 
28
 
 
29
 
 
30
RingBuffer::RingBuffer() : m_buffer( 0 ),
 
31
                   m_size( 1 ),
 
32
                   m_read_index( 0 ),
 
33
                   m_write_index( 0 )
 
34
{
 
35
    // A better start value? They'll grow as needed anyway.
 
36
    m_buffer = (char*) malloc( m_size );
 
37
}
 
38
 
 
39
 
 
40
void
 
41
RingBuffer::write( const QByteArray& buffer )
 
42
{
 
43
    Q_DEBUG_BLOCK << "towrite:" << buffer.size();
 
44
    qDebug() << "used" << used();
 
45
    qDebug() << "free" << free();
 
46
    qDebug() << "size" << size();
 
47
 
 
48
    int length = buffer.size();
 
49
    const char* src = buffer.data();
 
50
    const uint free = this->free();
 
51
 
 
52
    if (length > (int)free)
 
53
        expandBy( length - free + 1 );
 
54
 
 
55
    for (int cnt; length > 0; )
 
56
    {
 
57
        cnt = qMin( length, m_size - m_write_index );
 
58
        memcpy( m_buffer + m_write_index, src, cnt );
 
59
        m_write_index = (m_write_index + cnt) % m_size;
 
60
        length -= cnt;
 
61
        src += cnt;
 
62
    }
 
63
}
 
64
 
 
65
 
 
66
void
 
67
RingBuffer::read( QByteArray& tofill )
 
68
{
 
69
    Q_DEBUG_BLOCK << "toread:" << tofill.size();
 
70
    qDebug() << "used" << used();
 
71
    qDebug() << "free" << free();
 
72
    qDebug() << "size" << size();
 
73
 
 
74
    if (tofill.size() > used())
 
75
        tofill.resize( used() );
 
76
 
 
77
    int length = tofill.size();
 
78
    char* out = tofill.data();
 
79
 
 
80
    while (length > 0)
 
81
    {
 
82
        int cnt = qMin( length, m_size - m_read_index );
 
83
        memcpy( out, m_buffer + m_read_index, cnt );
 
84
        m_read_index = (m_read_index + cnt) % m_size;
 
85
        length -= cnt;
 
86
        out += cnt;
 
87
    }
 
88
}
 
89
 
 
90
 
 
91
void
 
92
RingBuffer::expandBy( uint amount )
 
93
{
 
94
    //Q_DEBUG_BLOCK << amount;
 
95
 
 
96
    char* tmp = (char*)realloc( m_buffer, sizeof(char) * ( m_size + amount ) );
 
97
 
 
98
    if (!tmp) {
 
99
        Q_ASSERT( !"Could not reallocate buffer!" );
 
100
        return;
 
101
    }
 
102
 
 
103
    m_buffer = tmp;
 
104
 
 
105
    // Don't screw up the read order or ugly noises will ensue
 
106
    if (m_read_index > m_write_index)
 
107
    {
 
108
        memmove( m_buffer + m_read_index + amount,
 
109
                 m_buffer + m_read_index,
 
110
                 m_size - m_read_index );
 
111
        m_read_index += amount;
 
112
    }
 
113
 
 
114
    m_size += amount;
 
115
 
 
116
    //qDebug() << m_size;
 
117
}